Google-api-php-client: setAccesstoken not changing the accesstoken used

Created on 23 Jun 2019  路  3Comments  路  Source: googleapis/google-api-php-client

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.

Environment details

  • OS: Windows 10 x64
  • PHP version: 7.1
  • Package name and version: "google/apiclient": "^2.0"

Steps to reproduce

  1. Setup the Google_Client
  2. set the first accesstoken and make an api request
  3. set a second accesstoken and make another api request, it will fail and still use token1

Code example

<?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);
question

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:

$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.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mevsme picture mevsme  路  4Comments

whatido1 picture whatido1  路  3Comments

camohub picture camohub  路  3Comments

kungufli picture kungufli  路  3Comments

slaFFik picture slaFFik  路  5Comments