RefreshToken在获得新的令牌谷歌表单API后没有得到回复 [英] RefreshToken Not getting send back after I get new token google sheets API

查看:399
本文介绍了RefreshToken在获得新的令牌谷歌表单API后没有得到回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有PHP的Google表单API,并遵循可在此处找到的快速入门指南 https://developers.google.com/sheets/quickstart/php



当我正确授权并将后续json文件存储在特定路径中

  {
access_token:xxxxxxx,
token_type:承载者,
expires_in:3600,
refresh_token:xxxxxx,
created:1472731452
}

在此过期后,我的代码中会触发以下内容:

  if $ client-> isAccessTokenExpired()){
$ client-> fetchAccessTokenWithRefreshToken(
$ client-> getRefreshToken()
);

$ this->文件系统
- > put(
self :: CREDENTIALS,
json_encode($ client-> getAccessToken())
);

$ / code>

现在我的问题是当代码被触发时它会将我的文件更新为

  {
access_token:xxxxxxx,
token_type:持票人,
expires_in:3600,
创建:1472731452
}

正如您所看到的,不再有刷新标记。当这个令牌到期时,我开始得到以下错误:

pre $ [code] [LogicException]
刷新令牌必须被传入或设置为部分setAccessToken

这是完全可以理解的,因为我再也没有刷新令牌。 p>

我的问题是为什么刷新令牌被删除?我打电话的方法与快速入门指南 https://developers.google.com / sheets / quickstart / php



我在指南中详细介绍了这部分内容

  //如果令牌过期,刷新令牌。 
if($ client-> isAccessTokenExpired()){
$ client-> fetchAccessTokenWithRefreshToken($ client-> getRefreshToken());
file_put_contents($ credentialsPath,json_encode($ client-> getAccessToken()));


解决方案

Your 刷新令牌已过期,因为代码中设置的生命周期为3600

  {
access_token:xxxxxxx,
token_type :Bearer,
expires_in:3600,// refresh_token 1小时有效
refresh_token:xxxxxx,
创建:1472731452
}

使用刷新令牌



刷新令牌是在第一次授权代码交换期间在离线场景中获得的。在这些情况下,您的应用程序可能会通过向Google OAuth 2.0授权服务器发送刷新令牌来获取新的访问令牌。



要以这种方式获取新的访问令牌,应用程序会向HTTPS POST请求发送 https://www.googleapis.com/oauth2/v4/token 。该请求必须包含以下参数:

这样的请求看起来类似于以下内容:

  POST / oauth2 / v4 /令牌HTTP / 1.1 
主机:www.googleapis.com
内容类型:application / x-www-form-urlencoded

client_id = 8819981768.apps.googleusercontent.com&
client_secret = {client_secret}&
refresh_token = 1 / 6BMfW9j53gdGImsiyUH5kU5RsR4zwI9lUVX-tqf8JXQ&
grant_type = refresh_token

检查 SO线程以获取其他参考。
希望这有助于!


I am using the google sheets API with PHP and followed the quick start guide that can be found over here https://developers.google.com/sheets/quickstart/php

When I do authorize properly and store the follow json file in a speicfied path

{
  "access_token": "xxxxxxx",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "xxxxxx",
  "created": 1472731452
}

After this expires the following gets triggered in my code

if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken(
        $client->getRefreshToken()
    );

    $this->filesystem
        ->put(
            self::CREDENTIALS,
            json_encode($client->getAccessToken())
        );
}

Now my issue is when that code gets triggered it will update my file to something like the following.

{
  "access_token": "xxxxxxx",
  "token_type": "Bearer",
  "expires_in": 3600,
  "created": 1472731452
}

As you can see there is no refresh token anymore. When this token expires I start getting the following error

[LogicException]
refresh token must be passed in or set as part of setAccessToken

Which is perfactly understandable because I don't have the refresh token there anymore.

My question is why is the refresh token getting removed? I am call the same methods as the one in the quick start guide https://developers.google.com/sheets/quickstart/php

I am talking specificaly about this part in the guide

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

解决方案

Your refresh token expired because the lifespan set in your code was 3600 seconds only/1 hour.

{
"access_token": "xxxxxxx",
"token_type": "Bearer",
"expires_in": 3600, //refresh_token good for 1 hour
"refresh_token": "xxxxxx",
"created": 1472731452
}

Using a refresh token

A refresh token is obtained in offline scenarios during the first authorization code exchange. In these cases, your application may obtain a new access token by sending a refresh token to the Google OAuth 2.0 Authorization server.

To obtain a new access token this way, your application sends an HTTPS POST request to https://www.googleapis.com/oauth2/v4/token. The request must include the following parameters:

Such a request will look similar to the following:

POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded

client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
refresh_token=1/6BMfW9j53gdGImsiyUH5kU5RsR4zwI9lUVX-tqf8JXQ&
grant_type=refresh_token

Check this SO thread for additional reference. Hope this helps!

这篇关于RefreshToken在获得新的令牌谷歌表单API后没有得到回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆