Google PHP SDK不会更改setAccessToken上的Accesstoken

时间:2019-06-23 11:59:35

标签: php google-api-php-client google-authentication google-my-business-api

我正在构建一个小型工具来一次管理多个GMB位置,但是我遇到了google php api client的问题。

我正在从数据库中获取多个用户/位置的访问令牌,并希望在一个循环中对其进行更新,但是google php api客户端在第二次使用setAccesstoken的请求中并未更改已使用的访问令牌时间。

即使getAccesstoken()返回正确的访问令牌,在第一次运行之后的每次循环运行仍将使用第一次访问的访问令牌。似乎内部使用的GuzzleClient不会被更新以将新令牌用于下一个请求。

不幸的是,我找不到强制sdk更新GuzzleClient或重新创建它的方法。我希望你能帮助我。

 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);
        }

    }

}

编辑: 我举了另一个示例,从我自己的代码中删除了所有变量。请求1正常,请求2失败,因为它仍然使用$token1,如果我删除请求1,请求2正常。

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

1 个答案:

答案 0 :(得分:1)

这似乎是Google PHP API客户端中的错误/不存在的功能。

我在github上报告了一个问题,希望在那里弄清楚。

https://github.com/googleapis/google-api-php-client/issues/1672

编辑:

在Github,我得到了reply并提供了解决问题的方法。设置新的访问令牌后,您需要手动清除缓存。

  

嗨@ Fredyy90,

     

客户端将为访问同一服务的呼叫缓存访问令牌   相同的范围。要更改令牌,您可以清除此缓存:

$client->setAccessToken($token1); // <call 1>

$client->getCache()->clear();

$client->setAccessToken($token2); // <call 2> 
  

我们将了解我们的方式   可以使这种行为更清晰并向人们提供更多控制   希望修改令牌。