无法从 google api OAuth2 刷新令牌

Cannot refresh token from google api OAuth2

这是我的 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-qRefreshToken.php

PHP 注意:未定义 属性: stdClass::$refresh_token in /var/www/googleapi/RefreshToken.php on line 20

在 Google OAuth2 中默认不返回 refresh_token

请求授权码需要一个额外的参数(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)