我已经建立了一个原型日历同步系统使用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错误
发布评论