/** * (non-PHPdoc) * * @see PostmanZendModuleTransport::createZendMailTransport() */ public function createZendMailTransport($fakeHostname, $fakeConfig) { if (PostmanOptions::AUTHENTICATION_TYPE_OAUTH2 == $this->getAuthenticationType()) { $config = PostmanOAuth2ConfigurationFactory::createConfig($this); } else { $config = PostmanBasicAuthConfigurationFactory::createConfig($this); } // Google's autoloader will try and load this so we list it first require_once 'PostmanGmailApiModuleZendMailTransport.php'; // Gmail Client includes require_once 'google-api-php-client-1.1.2/src/Google/Client.php'; require_once 'google-api-php-client-1.1.2/src/Google/Service/Gmail.php'; // build the Gmail Client $authToken = PostmanOAuthToken::getInstance(); $client = new Postman_Google_Client(); $client->setClientId($this->options->getClientId()); $client->setClientSecret($this->options->getClientSecret()); $client->setRedirectUri(''); // rebuild the google access token $token = new stdClass(); $token->access_token = $authToken->getAccessToken(); $token->refresh_token = $authToken->getRefreshToken(); $token->token_type = 'Bearer'; $token->expires_in = 3600; $token->id_token = null; $token->created = 0; $client->setAccessToken(json_encode($token)); // We only need permissions to compose and send emails $client->addScope("https://www.googleapis.com/auth/gmail.compose"); $service = new Postman_Google_Service_Gmail($client); $config[PostmanGmailApiModuleZendMailTransport::SERVICE_OPTION] = $service; return new PostmanGmailApiModuleZendMailTransport(self::HOST, $config); }
public function getClient() { $client = new Postman_Google_Client(); $client->setDeveloperKey(self::KEY); if (strlen($this->token)) { $client->setAccessToken($this->token); } if (strlen($this->memcacheHost)) { $client->setClassConfig('Postman_Google_Cache_Memcache', 'host', $this->memcacheHost); $client->setClassConfig('Postman_Google_Cache_Memcache', 'port', $this->memcachePort); } return $client; }
$dr_service = new Postman_Google_Service_Drive($client); /************************************************ Boilerplate auth management - see user-example.php for details. ************************************************/ if (isset($_REQUEST['logout'])) { unset($_SESSION['access_token']); } if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['access_token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $client->setAccessToken($_SESSION['access_token']); } else { $authUrl = $client->createAuthUrl(); } /************************************************ If we're signed in, retrieve channels from YouTube and a list of files from Drive. ************************************************/ if ($client->getAccessToken()) { $_SESSION['access_token'] = $client->getAccessToken(); $dr_results = $dr_service->files->listFiles(array('maxResults' => 10)); $yt_channels = $yt_service->channels->listChannels('contentDetails', array("mine" => true)); $likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes; $yt_results = $yt_service->playlistItems->listPlaylistItems("snippet", array("playlistId" => $likePlaylist)); } echo pageHeader("User Query - Multiple APIs");
$client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); $service = new Postman_Google_Service_Drive($client); if (isset($_REQUEST['logout'])) { unset($_SESSION['upload_token ']); } if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['upload_token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) { $client->setAccessToken($_SESSION['upload_token']); if ($client->isAccessTokenExpired()) { unset($_SESSION['upload_token']); } } else { $authUrl = $client->createAuthUrl(); } /************************************************ If we're signed in then lets try to upload our file. ************************************************/ if ($client->getAccessToken()) { $file = new Postman_Google_Service_Drive_DriveFile(); $file->title = "Big File"; $chunkSizeBytes = 1 * 1024 * 1024; // Call the API with the media upload, defer so it doesn't immediately return.
/** * This is the only place where the Google library is loaded * * @see PostmanTransport::createZendMailTransport() */ public function createZendMailTransport($hostname, $config) { // This is where the ZendMail special transport is loaded require_once 'PostmanGmailApiModuleZendMailTransport.php'; // This is the only place where the Google library is loaded require_once 'google-api-php-client-1.1.2/src/Google/Client.php'; require_once 'google-api-php-client-1.1.2/src/Google/Service/Gmail.php'; // build the Gmail Client $options = PostmanOptions::getInstance(); $authToken = PostmanOAuthToken::getInstance(); $client = new Postman_Google_Client(); $client->setClientId($options->getClientId()); $client->setClientSecret($options->getClientSecret()); $client->setRedirectUri(''); // rebuild the google access token $token = new stdClass(); $token->access_token = $authToken->getAccessToken(); $token->refresh_token = $authToken->getRefreshToken(); $token->token_type = 'Bearer'; $token->expires_in = 3600; $token->id_token = null; $token->created = 0; $client->setAccessToken(json_encode($token)); // We only need permissions to compose and send emails $client->addScope("https://www.googleapis.com/auth/gmail.compose"); $service = new Postman_Google_Service_Gmail($client); $config[PostmanGmailApiModuleZendMailTransport::SERVICE_OPTION] = $service; return new PostmanGmailApiModuleZendMailTransport($hostname, $config); }
if ($client_id == '<YOUR_CLIENT_ID>' || !strlen($service_account_name) || !strlen($key_file_location)) { echo missingServiceAccountDetailsWarning(); } $client = new Postman_Google_Client(); $client->setApplicationName("Client_Library_Examples"); $service = new Postman_Google_Service_Books($client); /************************************************ If we have an access token, we can carry on. Otherwise, we'll get one with the help of an assertion credential. In other examples the list of scopes was managed by the Client, but here we have to list them manually. We also supply the service account ************************************************/ if (isset($_SESSION['service_token'])) { $client->setAccessToken($_SESSION['service_token']); } $key = file_get_contents($key_file_location); $cred = new Postman_Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/books'), $key); $client->setAssertionCredentials($cred); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $_SESSION['service_token'] = $client->getAccessToken(); /************************************************ We're just going to make the same call as in the simple query as an example. ************************************************/ $optParams = array('filter' => 'free-ebooks'); $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); echo "<h3>Results Of Call:</h3>";
public function testSettersGetters() { $client = new Postman_Google_Client(); $client->setClientId("client1"); $client->setClientSecret('client1secret'); $client->setState('1'); $client->setApprovalPrompt('force'); $client->setAccessType('offline'); $client->setRedirectUri('localhost'); $client->setApplicationName('me'); $this->assertEquals('object', gettype($client->getAuth())); $this->assertEquals('object', gettype($client->getCache())); $this->assertEquals('object', gettype($client->getIo())); $client->setAuth(new Postman_Google_Auth_Simple($client)); $client->setAuth(new Postman_Google_Auth_OAuth2($client)); try { $client->setAccessToken(null); die('Should have thrown an Postman_Google_Auth_Exception.'); } catch (Postman_Google_Auth_Exception $e) { $this->assertEquals('Could not json decode the token', $e->getMessage()); } $token = json_encode(array('access_token' => 'token')); $client->setAccessToken($token); $this->assertEquals($token, $client->getAccessToken()); }