简体   繁体   English

无法从Google API OAuth2刷新令牌

[英]Cannot refresh token from google api OAuth2

Here is my php script RefreshToken.php 这是我的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";
?>

Run PHP CLI 运行PHP CLI

php -q RefreshToken.php php -q RefreshToken.php

PHP Notice: Undefined property: stdClass::$refresh_token in /var/www/googleapi/RefreshToken.php on line 20 PHP注意:第20行的/var/www/googleapi/RefreshToken.php中未定义的属性:stdClass :: $ refresh_token

The refresh_token is not returned by default in Google OAuth2. 默认情况下,Google OAuth2不返回refresh_token

The request for an authorization code requires an extra parameter ( access_type ): then refresh token will be returned with access_token. 授权码请求需要一个额外的参数( access_type ):然后刷新令牌将与access_token一起返回。

Other unusual behaviour: the refresh_token is returned only one time for a user. 其他异常行为: refresh_token对于用户仅返回一次。 If, for some reason, refresh_token is lost for that user, then user will need to open Google Account Security Settings page and drop access for your app. 如果由于某种原因丢失了该用户的refresh_token ,则该用户将需要打开Google帐户安全设置页面并放弃对您的应用的访问权限。 And this is not enough: your app will need to pass more one params ( approval_prompt equals true ) to indicate we're forcing the request for a new refresh_token . 但这还不够:您的应用将需要传递更多一个参数( approval_prompt等于true )来表明我们正在强制要求新的refresh_token

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM