B
B
Bogdan Pasechnik2016-02-29 12:20:47
PHP
Bogdan Pasechnik, 2016-02-29 12:20:47

Google Calendar OAuth 2.0. For what reason can a refresh token fail?

Set up integration with google calendar. Used the library for php google/apiclient. The standard code is

function getClient()
{
     $client = new Google_Client();
     $client->setApplicationName(APP_NAME);
     $client->setScopes('https://www.googleapis.com/auth/calendar');
     $client->setClientId(CLIENT_ID);
     $client->setClientSecret(CLIENT_SECRET);
     $client->setAccessType('offline');
     $client->setRedirectUri('RETURN_URL');
     $client->setApprovalPrompt('force');
     return $client;
}
// $refresh_token - ранее полученный токен
$client = getClient();
$client->refreshToken($refresh_token);
$service = new \Google_Service_Calendar($client);
// далее работаю с календарем

As you can see I'm only using refresh_token. And I generate an access token dynamically upon request. I can't say that this is the right decision. But I figured that since requests to the Google calendar will not be frequent, it makes no sense to fool around with saving the access token. And I just generate it dynamically.
Now what is my problem. For some accounts, refresh_token flies and Google starts returning an error - "error" : "invalid_grant".
As far as I understand, refresh_token should not have an expiration date.
The code for integration via OAuth 2.0 is as follows.
echo getClient()->createAuthUrl();
// далее в RETURN_URL разбираю ответ и сохраняю $refresh_token

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Zinchenko, 2016-03-04
@lisiy50

https://developers.google.com/identity/protocols/O... here is the answer.
The refresh_token has both an expiration time and the number of times it can generate an access_token (limit 25). In the code, the access_token is updated every time, in theory, after 25 api requests, the refresh_token will become invalid. I made more requests, everything worked fine.
It turns out that the logic should be as follows:
Correct me if I'm wrong.
Now the question is different: the documentation says 25 for the refresh_token usage limit, I did more, why does it continue to work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question