Google 客户端:$client->isAccessTokenExpired() 始终返回 true

jrs*_*gtr 5 php google-oauth youtube-data-api

我编写了一个方法来获取 Google 客户端并在需要时刷新令牌文件。但是$client->isAccessTokenExpired()总是返回 true,因此每个请求都会转储一个新令牌。

我已经读过这个问题了。答案不适用于我。我已经按照正确的顺序执行了这些步骤。

希望有人能引导我走向正确的方向。

/**
 * 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();
}
Run Code Online (Sandbox Code Playgroud)