0
votes

Basically, I've been using the PHP Google API Client library for accessing gmail inbox of multiple users(who've allowed access through a oAuth2 workflow) and extracting some data...

Observations: The Client gets authenticated properly only after 10 to 15 mins of time... Once a token is used to authenticate a user's gmail inbox, data is accessible for that user. but when the next token is used to authenticate another user's gmail inbox, the PHP google api client doesn't reload but uses the previous user(I suspect it's been cached / stored in session).

Code flow:

<?php
$tokenArray = array();
foreach($tokenArray as $token) {
 $googleclient = getGoogleClient($token);
 //$googleclient is stale even after the second iteration of the foreach loop
 $service = new Google_Service_Gmail($googleclient);
 $googleuser = "me";
 //if we change the variable $googleuser to some email address "[email protected]", we get a "Delegation denied for [email protected]"
 $userprofile = $service->users->getProfile($googleuser);
 var_dump($userprofile);//same data
}
?>

if we take it out of the loop and make separate calls after 25 - 30 mins(or restart the apache webserver), The second token is used in the client library properly.

Is it because the Google API PHP Client Library is caching the object and reusing it for 30 mins or am I missing something?

My question is what should I do to reload the client while handling more than one token?

Is there a proper way / any other way to initialize the client?

Do any of you have any code snippets for PHP Google API Client library... I've checked it in the official docs but to no avail...

Thanks for any help

1

1 Answers

0
votes

This issue is fixed after I've removed the "php client library" and downloaded a fresh set from git "git clone -b v1-master https://github.com/google/google-api-php-client.git "

and linked the autoload.php file from the downloaded set...("google-api-php-client/src/Google/autoload.php")

Previously I was using Composer to build it... but now I didn't use Composer at all and the Google API "PHP Client Library" worked just fine...


Also I faced new issues...

Issue 1) Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Could not json decode the token' in google-api-php-client/src/Google/Auth/OAuth2.php'

Solution: instead of passing an associated array of the token string, pass the token string as it is... $client->setAccessToken($tokenstring); then it is solved. also make sure to json_encode() the string returned from $client->getAccessToken(); before storing it in database or file

Issue 2) Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_grant: Code was already redeemed.'

Solution: previously, google api seemed to return the error message as associated array when we call the $client->authenticate($code); but now it throws an error... So Error Handling with try{}catch() solved the issue

the source of issue 2) is firefox's prefetch feature...

firefox tried to hit the url twice for prefetching and it raised the exception ... still trying to overcome issue (2)