这是我的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注意:第20行的/var/www/googleapi/RefreshToken.php中未定义的属性:stdClass :: $ refresh_token
在refresh_token
默认情况下不会在谷歌的OAuth2返回。
请求授权码需要一个额外的参数(access_type
):然后刷新令牌将与access_token一起返回。
其他异常行为:refresh_token
仅对用户返回一次。如果由于某种原因refresh_token
丢失了该用户,则该用户将需要打开Google帐户安全设置页面并放弃对您的应用程序的访问权限。但这还不够:您的应用将需要传递更多一个参数(approval_prompt
等于true
),以表明我们正在强制要求新的参数refresh_token
。
更多信息:https : //developers.google.com/accounts/docs/OAuth2WebServer#offline)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句