Google Client: $client->isAccessTokenExpired() always returns true

Viewed 455

I wrote a method to get the Google client and refresh the token file when needed. However $client->isAccessTokenExpired() always returns true, so every request a new token gets dumped.

I have already read this question. The answer does not apply to me. I already do the steps in the right order.

Hopefully someone can steer me in the right direction.

/**
 * 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();
}
0 Answers
Related