dreamfly2016 2015-06-15 15:09
浏览 78
已采纳

Google PHP SDK - 无法获取刷新令牌

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?

  • 写回答

2条回答 默认 最新

  • dqthn68688 2015-06-15 15:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?