微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Google OAuth2错误 – 缺少必需参数:刷新时为grant_type

我使用Google日历API构建了一个原型日历同步系统,除了刷新访问令牌外,它运行良好.这些是我经历的步骤:

1)授权我的API并收到授权码.

2)交换了访问令牌和RefreshToken的授权码.

3)使用Calendar API直到访问令牌过期.

此时,我尝试使用刷新令牌获取一个访问令牌,因此我的用户不必继续授予访问权限,因为日记同步在脱机时发生.

这是PHP代码,我在整个系统中使用curl请求.

$requestURL = "https://accounts.google.com/o/oauth2/token";
$postData = array("grant_type" => "refresh_token", 
                  "client_id" => $clientID,    
                  "client_secret" => $clientSecret, 
                  "refresh_token" => $refreshToken);

$headers[0] = 'Content-Type: application/json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));

$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);

我得到的回应是:

[error] => invalid_request
[error_description] => required parameter is missing: grant_type

没有报告卷曲错误.

我已尝试过标题内容类型:application / x-www-form-urlencoded,以及许多其他内容,结果相同.

我怀疑它在我的卷曲设置或标题中显而易见,因为此文档中提到的此请求中提到的每个参数都已设置.但是,我会绕圈子去,所以会感谢任何帮助,包括指出我忽略的任何明显错误.

解决方法:

您的请求不应发布JSON数据,而应查询表单编码数据,如:

$requestURL = "https://accounts.google.com/o/oauth2/token";
$postData = "grant_type=refresh_token&client_id=$clientID&client_secret=$clientSecret&refresh_token=$refreshToken";

$headers[0] = 'Content-Type: application/x-www-form-urlencoded';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐