I have a problem with the refresh tokens google returns. I think they expire after some days. Documentation says that they do not unless the user does this manually.
Here is what I do:
I prompt the user to authorize my app using Google_Client
$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_REDIRECT_URI);
$client->setScopes(array(GOOGLE_SCOPE_A,'https://www.googleapis.com/auth/userinfo.email'));
$client->setApplicationName(GOOGLE_APPLICATION_NAME);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
I store the refresh token in the database using
$refreshToken = $client->getRefreshToken();
I have a cURL that runs every day and pulls some data from analytics, it then connects using the following code:
$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->refreshToken($refreshToken);
$client->isAccessTokenExpired() //this returns false;
Everything works fine for the first 15-30 days. After that, it looks like the refresh token is not working any more.
$client->isAccessTokenExpired() //this returns true;
and when I am trying to pull data from analaytics I am getting a Google_Service_Exception with code:401 and the message:"Login Required"
What I do then is prompt the user to authorize my app again, update the refresh token with the new one in the database and everything is working as expected for the next 15-30 days until the above problem happens again.
Does anyone have any idea why this is happening and how can I prevent this?
Thank you.