public function testFileCache() { $this->onlyPhp55AndAbove(); $this->checkServiceAccountCredentials(); $client = new Google_Client(); $client->useApplicationDefaultCredentials(); $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); // filecache with new cache dir $cache = $this->getCache(sys_get_temp_dir() . '/cloud-samples-tests-php-cache-test/'); $client->setCache($cache); $token1 = null; $client->setTokenCallback(function ($cacheKey, $accessToken) use($cache, &$token1) { $token1 = $accessToken; $cacheItem = $cache->getItem($cacheKey); // expire the item $cacheItem->expiresAt(new DateTime('now -1 second')); $cache->save($cacheItem); $cacheItem2 = $cache->getItem($cacheKey); }); /* Refresh token when expired */ if ($client->isAccessTokenExpired()) { $client->refreshTokenWithAssertion(); } /* Make a service call */ $service = new Google_Service_Drive($client); $files = $service->files->listFiles(); $this->assertInstanceOf('Google_Service_Drive_FileList', $files); sleep(1); // make sure the token expires $client = new Google_Client(); $client->useApplicationDefaultCredentials(); $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); $client->setCache($cache); $token2 = null; $client->setTokenCallback(function ($cacheKey, $accessToken) use(&$token2) { $token2 = $accessToken; }); /* Make another service call */ $service = new Google_Service_Drive($client); $files = $service->files->listFiles(); $this->assertInstanceOf('Google_Service_Drive_FileList', $files); $this->assertNotEquals($token1, $token2); }
private function createClient() { $options = ['auth' => 'google_auth', 'exceptions' => false]; if ($proxy = getenv('HTTP_PROXY')) { $options['proxy'] = $proxy; $options['verify'] = false; } // adjust constructor depending on guzzle version if (!$this->isGuzzle6()) { $options = ['defaults' => $options]; } $httpClient = new GuzzleHttp\Client($options); $client = new Google_Client(); $client->setApplicationName('google-api-php-client-tests'); $client->setHttpClient($httpClient); $client->setScopes(["https://www.googleapis.com/auth/plus.me", "https://www.googleapis.com/auth/urlshortener", "https://www.googleapis.com/auth/tasks", "https://www.googleapis.com/auth/adsense", "https://www.googleapis.com/auth/youtube", "https://www.googleapis.com/auth/drive"]); if ($this->key) { $client->setDeveloperKey($this->key); } list($clientId, $clientSecret) = $this->getClientIdAndSecret(); $client->setClientId($clientId); $client->setClientSecret($clientSecret); $client->setCache($this->getCache()); return $client; }
/** * Returns an instance of Google_Client. Automatically handles set up of the * client, including: * - authentication using configuration in SiteConfig * - caching support via an implementation of the caching interface defined * by the Google API. */ public static function get_client() { $client = new Google_Client(); // If we are going through a proxy, set that up $proxy = static::get_config('external_proxy'); if ($proxy) { $io = $client->getIo(); $parts = static::decode_address($proxy, 'http', '80'); $io->setOptions(array(CURLOPT_PROXY => $parts['Address'], CURLOPT_PROXYPORT => $parts['Port'])); } $client->setClientId(static::get_config('client_id')); $client->setApplicationName(static::get_config('application_name')); // absolute if starts with '/' otherwise relative to site root $keyPathName = static::get_config('private_key_file'); if (substr($keyPathName, 0, 1) !== '/') { $keyPathName = Director::baseFolder() . "/" . $keyPathName; } $client->setAssertionCredentials(new Google_Auth_AssertionCredentials(static::get_config('service_account'), explode(',', static::get_config('scopes')), file_get_contents($keyPathName))); $client->setAccessType('offline_access'); // Set up custom database cache handler. $client->setCache(new GoogleAPICacheHandler()); return $client; }
public function testSettersGetters() { $client = new Google_Client(); $client->setClientId("client1"); $client->setClientSecret('client1secret'); $client->setState('1'); $client->setApprovalPrompt('force'); $client->setAccessType('offline'); $client->setRedirectUri('localhost'); $client->setConfig('application_name', 'me'); $client->setCache($this->getMock('Psr\\Cache\\CacheItemPoolInterface')); $this->assertEquals('object', gettype($client->getCache())); try { $client->setAccessToken(null); $this->fail('Should have thrown an Exception.'); } catch (InvalidArgumentException $e) { $this->assertEquals('invalid json token', $e->getMessage()); } $token = array('access_token' => 'token'); $client->setAccessToken($token); $this->assertEquals($token, $client->getAccessToken()); }