本文整理汇总了PHP中Google_Client::setTokenCallback方法的典型用法代码示例。如果您正苦于以下问题:PHP Google_Client::setTokenCallback方法的具体用法?PHP Google_Client::setTokenCallback怎么用?PHP Google_Client::setTokenCallback使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Google_Client
的用法示例。
在下文中一共展示了Google_Client::setTokenCallback方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testFileCache
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);
}