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 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿