Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.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 API PHP刷新令牌返回NULL_Php_Google Api_Google Api Php Client - Fatal编程技术网

Google API PHP刷新令牌返回NULL

Google API PHP刷新令牌返回NULL,php,google-api,google-api-php-client,Php,Google Api,Google Api Php Client,我正在使用Google API Client Library for PHP进行离线身份验证,但当我使用以下代码时: $this->google->refreshToken($result["google_refresh_token"])); 它返回NULL $this->google引用了google_客户端类的一个实例 有人知道这是什么吗 如果我试图更改给定的刷新令牌,它将返回一个Google异常错误,因此它应该是一个有效的令牌 谢谢大家! 编辑: 也许这对你有帮助我也有同样的

我正在使用Google API Client Library for PHP进行离线身份验证,但当我使用以下代码时:

$this->google->refreshToken($result["google_refresh_token"]));
它返回NULL

$this->google
引用了google_客户端类的一个实例

有人知道这是什么吗

如果我试图更改给定的刷新令牌,它将返回一个Google异常错误,因此它应该是一个有效的令牌

谢谢大家!

编辑:


也许这对你有帮助

我也有同样的问题很久了。对我来说,这是因为,出于某种原因,谷歌只在您第一次请求刷新令牌时才向您发送刷新令牌。所以你必须确保你为那个用户保存了刷新令牌,因为谷歌不会再给你了

对于发展来说,这可能是一个真正的痛苦。你可以让谷歌给你发送一个新的刷新令牌,但你要做的是取消你通过应用程序授权的任何帐户的授权。你可以在家里做这件事


希望这有帮助

我现在使用一个非常简单的方法来避免
refresh\u token()
方法。
我只保存第一个请求的整个JSON响应,并将其提供给
setAccesToken()
方法。如果访问令牌过期,它将自动从给定的JSON中获取一个新的令牌。

我也遇到了类似的问题。检查我的详细答案

这是一个快速的解决方案

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    $newAccessToken = $client->getAccessToken();
    $accessToken = array_merge($accessToken, $newAccessToken);
    file_put_contents($credentialsPath, json_encode($accessToken));
}

我的头撞在墙上很久了

这并不明显,但:

define('CREDENTIALS_PATH','/vendor/google/CREDENTIALS/CREDENTIALS.json')

引用“credentials.json”或任何您的凭据文件的行——启动时该文件不应该存在!如果您在获得授权之前创建了该文件,请删除该文件,然后访问脚本页面–
quickstart.php
或其他任何内容,然后它会要求您进行授权

如果您反复尝试刷新页面,则存在使用限制(即请求授权的次数过多),因此您可能需要在开发人员控制台中创建新的客户端id,并更新
define('client\u SECRET\u PATH')
PATH

我接着补充说:

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
    $jsonCred = file_get_contents(CREDENTIALS_PATH);
    $jsonArray = json_decode($jsonCred, true);

    $client->fetchAccessTokenWithRefreshToken($jsonArray["refresh_token"]);

    $newAccessToken = $client->getAccessToken();
    $accessToken = array_merge($jsonArray, $newAccessToken);

    file_put_contents($credentialsPath, json_encode($accessToken));
}

```

请添加更多上下文。。。$this google是如何构造的?它只是在config.php中启动和配置的一个新实例,并且第一个给定的acces令牌将一直工作到过期。应该有一些代码设置此类的google属性。。。仅凭您提供的数据,任何人都很难帮助您;)关于取消访问以获取新刷新令牌的伟大提示!太有用了。谢谢我使用了这个解决方案,它帮助了我
如果(文件\u存在($this->access\u token\u file\u location)){$this->google\u client->setAccessToken($this->access\u token);}
,其中$this->access\u token是
$access\u token=json\u decode($file\u content,true)
只需将先前保存的响应读取到文件中,读取该文件内容,并使用
json\u decode()将其转换为数组。
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
    $jsonCred = file_get_contents(CREDENTIALS_PATH);
    $jsonArray = json_decode($jsonCred, true);

    $client->fetchAccessTokenWithRefreshToken($jsonArray["refresh_token"]);

    $newAccessToken = $client->getAccessToken();
    $accessToken = array_merge($jsonArray, $newAccessToken);

    file_put_contents($credentialsPath, json_encode($accessToken));
}