这是我的php脚本RefreshToken.php
<?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::$refresh_token在/var/www/googleapi/refreshtoken . PHP第20行
Google OAuth2默认不返回refresh_token
。
请求授权码需要一个额外的参数(access_type
):那么刷新令牌将与access_token一起返回。
其他异常行为:一个用户只返回一次refresh_token
。如果由于某种原因,该用户丢失了refresh_token
,那么用户将需要打开谷歌帐户安全设置页面并放弃对您的应用程序的访问。这还不够:您的应用程序将需要传递更多一个参数(approval_prompt
等于true
),以表明我们正在强制请求新的refresh_token
。
更多信息:https://developers.google.com/accounts/docs/OAuth2WebServer#offline)