Working with the PHP Quickstart code, I found a problem: when the token needs to be refreshed, the code returns the following error:
Fatal error: Uncaug
Thanks to Alex Blex I was able to notice that the first time I receive the token has the refresh_token
but after the first request, the refresh_token
is not stored. The solution is the following (from this answer):
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$newAccessToken = $client->getAccessToken();
$accessToken = array_merge($accessToken, $newAccessToken);
file_put_contents($credentialsPath, json_encode($accessToken));
}
The refresh_token is only returned on the first request. When you refresh the access token a second time it returns everything except the refresh_token and the file_put_contents removes the refresh_token when this happens the second time.
Modifying the code as following will merge in the original access token with the new one. This way you will be able to preserve your refresh_token for future requests.