Google PHP SDK - 没有获取刷新令牌 [英] Google PHP SDK - not getting refresh token

查看:105
本文介绍了Google PHP SDK - 没有获取刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用PHP SDK获取Google API的刷新令牌。我使用Javascript验证用户,检索代码并将其交换到access_token服务器端,但这不会授予我访问令牌。我究竟做错了什么?这里是我使用的代码:

  $ client = new Google_Client(); 
$ client-> setClientId($ client_id);
$ client-> setClientSecret($ client_secret);
$ client-> addScope('https://www.googleapis.com/auth/plus.me');
$ client-> addScope('https://www.google.com/m8/feeds');
$ client-> setRedirectUri('postmessage');
$ client-> setAccessType('offline');

if(isset($ _ REQUEST ['code'])){
$ client-> authenticate($ _ REQUEST ['code']);
if($ client-> getAccessToken()){
$ _SESSION ['access_token'] = $ client-> getAccessToken();
$ token_data = $ client-> verifyIdToken() - > getAttributes();
$ result ['data'] = $ token_data;
$ result ['access_token'] = json_decode($ _ SESSION ['access_token']);
}
}

debug($ result); //我自己的函数,var_dumps数组的内容

这是数组的结果:<


access_token: TOKEN 已创建:1434380576

expires_in:3594

id_token: IDTOKEN

token_type :Bearer

如果我没有弄错第一个访问令牌应该包含刷新令牌,那么我做错了什么? / p>

解决方案

首先检查Google开发者控制台中的设置,看看您的RedirectUri是否相同并且API已激活(尽管如果您已经拥有该.json,那么我认为它是。



您必须至少1次通过Google Auth提示屏幕才能在您的.json,如果你的RedirectUri无处可去,你将无法得到你的刷新令牌,甚至不能访问已验证。

如果您正在进行小型文件交易,并且不需要用户验证脚本进程,则还可以尝试使用服务帐户。祝你好运。


I am trying to get a refresh token for the Google API's, using the PHP SDK. I am authenticating the user with Javascript, retrieving a code, and exchanging it for an access_token server side, but this doesn't grant me an access token. What am I doing wrong? Here is the code I use:

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->addScope('https://www.googleapis.com/auth/plus.me');
$client->addScope('https://www.google.com/m8/feeds');
$client->setRedirectUri('postmessage');
$client->setAccessType('offline');

if (isset($_REQUEST['code'])) {
    $client->authenticate($_REQUEST['code']);
    if ($client->getAccessToken()) {
        $_SESSION['access_token'] = $client->getAccessToken();
        $token_data = $client->verifyIdToken()->getAttributes();
        $result['data']=$token_data;
        $result['access_token']=json_decode($_SESSION['access_token']);
    }
}

debug($result); //my own function, var_dumps the content of an array

Here is the result of the array:

$result['access_token'] contains:

access_token: TOKEN
created: 1434380576
expires_in: 3594
id_token: IDTOKEN
token_type:"Bearer"

If I am not mistaken the first access token should also contain the refresh token, what am I doing wrong?

解决方案

First check the settings in the developer console of Google to see if your RedirectUri is the same and that the API is activated (although if you already got that .json, then I assume it is.

You have to go through the Google Auth Prompt Screen at least 1 time to get a refresh token in your .json, and if your RedirectUri is taking you nowhere, you won't be able to get your refresh token or even the access validated.

You can also try a service account if you're doing small file transactions and don't need a user validation for the process of your script. Good Luck.

这篇关于Google PHP SDK - 没有获取刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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