Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google PHP SDK未更改setAccessToken上的Accesstoken_Php_Google Api Php Client_Google Authentication_Google My Business Api - Fatal编程技术网

Google PHP SDK未更改setAccessToken上的Accesstoken

Google PHP SDK未更改setAccessToken上的Accesstoken,php,google-api-php-client,google-authentication,google-my-business-api,Php,Google Api Php Client,Google Authentication,Google My Business Api,我正在开发一个小工具来同时管理多个GMB位置,但我遇到了一个问题 我正在从数据库中获取多个用户/位置的AccessToken,并希望在循环中更新它们,但是google php api客户端在第二次使用时不会更改其请求中使用的AccessToken 在第一次运行之后,循环的每次运行都将继续使用第一次运行的accesstoken,即使getAccesstoken()返回正确的accesstoken。似乎内部使用的GuzzleClient没有得到更新,无法在下一个请求中使用新令牌 不幸的是,我找不到一

我正在开发一个小工具来同时管理多个GMB位置,但我遇到了一个问题

我正在从数据库中获取多个用户/位置的AccessToken,并希望在循环中更新它们,但是google php api客户端在第二次使用时不会更改其请求中使用的AccessToken

在第一次运行之后,循环的每次运行都将继续使用第一次运行的accesstoken,即使
getAccesstoken()
返回正确的accesstoken。似乎内部使用的
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);

这似乎是google php api客户端中的一个bug/不存在的功能

我在github上报告了一个问题,希望在那里得到澄清

编辑:

我在Github找到了解决问题的方法。设置新的accesstoken后,需要手动清除缓存

你好@fredy90

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

$client->setAccessToken($token1);//
$client->getCache()->clear();
$client->setAccessToken($token2);//
我们会看看我们的方法 可以使这种行为更清晰,并为人们提供更多的控制 正在查看如何修改令牌


看起来(在google客户端的
getHttpClient()
方法中)guzzle客户端在更改令牌后不会重新创建,我也没有找到任何方法可以强制客户端重置它。我认为谷歌客户端不是设计来使用这种方式的。您是否尝试为每个用户/令牌创建一个新的google客户端实例?创建新实例很好,但会产生我想要避免的不必要的开销,我来自facebook php sdk,它可以轻松地处理accesstoken更改。在设置新的访问令牌后,尝试调用客户端
authorize
方法。我不完全确定,但它似乎正在使用当前的访问令牌。如下所示:
$client->setAccessToken($token2)$客户端->授权(空)
authorize()
getHttpClient()
获取其http客户端,并且
getHttpClient()
一旦设置,就会返回
$this->http
,因此不幸的是,无法通过这种方式重新生成使用过的http客户端,设置新的accesstoken应该只设置
$this->http=null,一切都会很好,需要时会重新生成。所以authorize方法中的这一行没有任何区别,对吗<代码>$http=$authHandler->attachToken($http,$token,(数组)$scopes)
$client->setAccessToken($token1); // <call 1>

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

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