Php Google OAuth2错误-缺少必需的参数:刷新时授予类型

Php Google OAuth2错误-缺少必需的参数:刷新时授予类型,php,curl,oauth-2.0,google-calendar-api,google-oauth,Php,Curl,Oauth 2.0,Google Calendar Api,Google Oauth,我已经使用谷歌日历API构建了一个日历同步原型系统,除了刷新访问令牌外,它运行良好。以下是我所经历的步骤: 1) 授权我的API并收到授权代码 2) 将授权代码交换为访问令牌和刷新令牌 3) 在访问令牌过期之前一直使用日历API 此时,我尝试使用刷新令牌获得另一个访问令牌,这样我的用户就不必继续授予访问权限,因为日记同步是在他们脱机时进行的 这是PHP代码,我在整个系统中使用curl请求 $requestURL = "https://accounts.google.com/o/oauth2/to

我已经使用谷歌日历API构建了一个日历同步原型系统,除了刷新访问令牌外,它运行良好。以下是我所经历的步骤:

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

2) 将授权代码交换为访问令牌和刷新令牌

3) 在访问令牌过期之前一直使用日历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,以及其他许多东西,结果都是一样的


我怀疑这在我的curl设置或标题中很明显,因为Google文档中提到的每个参数都已设置好。然而,我正在兜圈子,所以我非常感谢任何帮助,包括指出我忽略的任何明显错误

您的请求不应该发布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);

谢谢你的回答。使用相同的结果错误尝试了该操作。请仔细检查:我刚刚验证了此确切代码,它返回:Array([access\u token]=>[token\u type]=>Bearer[expires\u in]=>3600[id\u token]=>)更改的不仅仅是内容类型,这也是postData本身以及它不能是json_encode'dmanny的事实。当我在同步程序中尝试它时,它不起作用,返回了相同的错误。我刚刚创建了一个独立的脚本,复制和粘贴上面的代码,它可以正常工作。所以,我要去某个地方。