I am trying to connect my web application with Google Drive, using the Google Drive API V2 and Google's PHP client library ("google/apiclient": "^2.0"
).
My application works fine for first hour. I can get files, uploads files, etc. but after 1 hour (when the access token expires and needs to be refreshed), subsequent gets/uploads result in a Google_Service_Exception (Err 400)
:
Your client has issued a malformed or illegal request.
I successfully obtain access & refresh tokens:
{
"access_token":"**********",
"expires_in":3600,
"refresh_token":"1/********",
"scope":"https://www.googleapis.com/auth/drive",
"token_type":"Bearer",
"created":1539085127
}
I have tried to refresh the token this way:
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}
And this way:
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
}
How can I properly maintain the credentials I received for more than an hour?