Answer the question
In order to leave comments, you need to log in
Why doesn't the Google API token refresh?
Good day.
I unwrapped Google's apish. I run a script with apiha in the php console, gives out a link to authorization - I go, I allow all the Google app, I get my code in the address bar and creates a toketn.json file - everything is generally fine here.
But a week later, the token dies and does not update itself. Why is that? What should be done?
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
if ($client->isAccessTokenExpired()) {
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
Answer the question
In order to leave comments, you need to log in
Check how you guard the refresh token, it must be a string without escape characters
1/xxxxxxxxxxxxxxx-ZZZ_yyyyyyyyyyyyy
and not
1\/xxxxxxxxxxxxxxx-ZZZ_yyyyyyyyyyyyy
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question