我写了一个方法来获取Google客户端并在需要时刷新令牌文件。但是$client->isAccessTokenExpired()
始终返回true,因此每个请求都会转储新令牌。
我已阅读this question。答案不适用于我。我已经按照正确的顺序执行了这些步骤。
希望有人能引导我朝着正确的方向前进。
/**
* Initialize Google Client and return it
* @return Client
*/
private function getClient()
{
if ( $this->client !== null ) {
return $this->client;
}
$client = new Client();
$client->setClientId($this->google_client_id);
$client->setClientSecret($this->google_client_secret);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setAccessToken(file_get_contents($this->google_token));
if($client->isAccessTokenExpired()) {
try {
$fs = new FileSystem();
$fs->dumpFile($this->google_token, json_encode($client->getAccessToken()));
$this->logger->info(sprintf('Dumped new Google OAuth token in: %s', $this->google_token));
} catch(IOException $e) {
$this->logger->critical(sprintf('Error dumping new Google OAuth token: %s', $e->getMessage());
}
}
$this->client = $client;
return $this->getClient();
}