2015-06-08 77 views
0

刷新令牌這裏是我的PHP腳本RefreshToken.php無法從谷歌API的OAuth2

<?php 
$url = 'https://accounts.google.com/o/oauth2/token'; 
$post_data = array(
        'code'   => 'xxxxxxxx', 
        'client_id'  => 'xxxxxxxxxxx', 
        'client_secret' => 'xxxxxxxxxxxxx', 
        'redirect_uri' => 'http://localhost/googleapi/AuthenticationCode.php', 
        'grant_type' => 'authorization_code', 
        ); 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$result = curl_exec($ch); 
$token = json_decode($result); 

echo $token->refresh_token . "\n"; 
?> 

運行PHP CLI

PHP -q RefreshToken.php

PHP公告:未定義的屬性:stdClass的: :第20行的/var/www/googleapi/RefreshToken.php中的$ refresh_token

+0

請在$結果的回聲......我的猜測是,$結果是一個數組不是對象... –

回答

1

refresh_token是n沒有在Google OAuth2中默認返回。

對授權碼的請求需要額外的參數(access_type):然後刷新令牌將與access_token一起返回。

其他異常行爲:refresh_token只返回給用戶一次。如果由於某種原因,該用戶丟失了refresh_token,則用戶需要打開Google Account Security Settings page並刪除您的應用的訪問權限。這還不夠:您的應用需要傳遞更多的參數(approval_prompt等於true),以表明我們正在強制請求新的refresh_token

更多信息:https://developers.google.com/accounts/docs/OAuth2WebServer#offline