Google OAuth2错误

编程入门 行业动态 更新时间:2024-10-09 09:14:52
本文介绍了Google OAuth2错误 - 缺少必需的参数:刷新时的grant_type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经建立了一个原型日历同步系统使用Google日历API,它工作得很好,除了刷新访问令牌。这些是我经历的步骤:

I have built a prototype calendar synching system using the Google calendar API and it works well, except refreshing access tokens. These are the steps I have gone through:

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

1) Authorised my API and received an authorisation code.

2)更改了访问令牌和RefreshToken的授权代码。

2) Exchanged the authorisation code for Access Token and a RefreshToken.

3)使用Google日历API,直到访问令牌过期。

3) Used the Calendar API until the Access Token expires.

刷新令牌获得另一个访问令牌,因此我的用户不必继续授予访问权限,因为日记同步在离线时发生。

At this point I try to use the Refresh Token to gain another Access Token, so my users don't have to keep granting access because the diary sync happens when they are offline.

这里是PHP代码在整个系统中使用curl请求。

Here's the PHP code, I'm using curl requests throughout the system.

$requestURL = "accounts.google/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

未报告卷曲错误。

我已经尝试过header content-type:application / x-www-form-urlencoded和很多其他的东西,结果相同。

I've tried header content-type: application/x-www-form-urlencoded, and many other things, with the same result.

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

I suspect it's something obvious in my curl settings or headers as every parameter mentioned in the Google documentation for this request is set. However, I'm going around in circles so would appreciate any help, including pointing out any obvious errors I've overlooked.

推荐答案

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

your request should not post JSON data but rather query form encoded data, as in:

$requestURL = "accounts.google/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);

更多推荐

Google OAuth2错误

本文发布于:2023-11-26 23:39:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635681.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!