Hey,
setAccesstoken doesnt update the used accesstoken for future api requests as expected, it only seems to work on the first time and later it updates Google_Client->$token but the GuzzleClient still uses the old token for all api requests.
<?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);
haven't you try with Refresh Token?
My complete code takes care of refreshing tokens, storing them and other stuff.
The given example code is just a very short example of the problem, expecting valid tokens.
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.
Most helpful comment
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:
We'll see about ways we can make this behavior clearer and provide more control to people looking to modify the tokens.