Im building a small Tool to manage multiple GMB Locations at once, but I run into a problem with the google php api client.
I'm fetching accesstokens of multiple users/locations from my database and want to update them in a loop, but the google php api client does not change the used accesstokens in its requests on using setAccesstoken for the second time.
Every run of the loop after the first will keep using the accesstoken from the first run, even though getAccesstoken()
return the correct accesstoken. seems like the internally used GuzzleClient
doesn't get updated to use the new token for the next requests.
Unfortunatly i couldn't find a method to force the sdk to update the GuzzleClient
or recreate it. I hope you can help me out.
foreach($this->getLocationsToUpdate() as $location){
$oldAccessToken = $this->google->getClient()->getAccessToken();
$placeData = $this->places->getFullPathAndToken($location);
if($placeData !== null){
try{
//only sets the token correct on the first run.
$this->google->setAccessToken($placeData['access_token']);
$this->updateReviews($placeData);
$this->updateQuestions($placeData);
$this->updateMedia($placeData);
// returns the correct token, but api requests in the methods above fail, since the auth header from the guzzle requests still use the token from the first run.
var_dump($this->google->getClient()->getAccessToken());
$this->google->setAccessToken($oldAccessToken);
}catch(\Exception $e){
$this->google->setAccessToken($oldAccessToken);
}
}
}
EDIT:
I made another example to remove all variables from my own code. Request 1 works fine, request 2 fails, because it still uses $token1
, if i remove request 1, request 2 works fine.
<?php
define('BASE_DIR', dirname(__FILE__));
require_once BASE_DIR.'/vendor/autoload.php';
$token1 = '.....';
$token2 = '.....';
$name1 = 'accounts/115224257627719644685/locations/12065626487534884042';
$name2 = 'accounts/115299736292976731655/locations/295582818900206145';
$client = new \Google_Client();
$client->setAuthConfig(BASE_DIR.'/Config/Google/credentials.json');
$client->setRedirectUri('https://...../login/callback.html');
$client->setAccessType("offline");
$client->setPrompt('consent');
$client->addScope(\Google_Service_Oauth2::USERINFO_EMAIL);
$client->addScope(\Google_Service_Oauth2::USERINFO_PROFILE);
$client->addScope("https://www.googleapis.com/auth/plus.business.manage");
// Request 1
$client->setAccessToken($token1);
$gmb = new \Google_Service_MyBusiness($client);
$media = $gmb->accounts_locations_media->listAccountsLocationsMedia($name1);
var_dump($media);
// Request 2 -- Fails because it still uses $token1
$client->setAccessToken($token2);
$gmb = new \Google_Service_MyBusiness($client);
$media = $gmb->accounts_locations_media->listAccountsLocationsMedia($name2);
var_dump($media);
This seems to be a bug/not existing feature in the google php api client.
I reported an issue on github and hope to get clarity over there.
https://github.com/googleapis/google-api-php-client/issues/1672
EDIT:
I got a reply with a solution for my problem, over at Github. You need to manually clear the cache after setting a new accesstoken.
Hi @Fredyy90,
The client will cache access tokens for calls to the same service with the same scopes. To change tokens, you can clear this cache:
$client->setAccessToken($token1); // <call 1>
$client->getCache()->clear();
$client->setAccessToken($token2); // <call 2>
We'll see about ways we can make this behavior clearer and provide more control to people looking to modify the tokens.