Google API OAuth 2.0 CURL返回"缺少必需参数:grant_type"

hud*_*hud 4 php curl oauth google-api

我正在尝试为Web服务器应用程序实施Google的OAuth 2.0身份验证.

我可以从Google ok获取代码,但是当我发回这个代码以尝试获取访问令牌时,它总是会给我错误"必需参数丢失:grant_type.错误400",即使grant_type存在.

此外,如果我将content-length指定为0以外的任何值,则会抛出其他错误.

这是执行此卷曲帖子的代码:

$url = 'https://accounts.google.com/o/oauth2/token';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_FAILONERROR, false);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded',
    'Content-length: 0'
));

curl_setopt($ch, CURLOPT_POSTFIELDS, array( 
    'code='. urlencode($code),
    'client_id=' . urlencode($clientID),
    'client_secret=' . urlencode($clientSecret),
    'redirect_uri=http%3A%2F%2Flocalhost%2Fexperiments%2FnewGALogin.php',
    'grant_type=authorization_code'
)); 
Run Code Online (Sandbox Code Playgroud)

Utk*_*rım 7

尝试

curl_setopt($ch, CURLOPT_POSTFIELDS, array( 
    'code' => $code,
    'client_id' => $clientID,
    'client_secret' => $clientSecret,
    'redirect_uri' => 'http%3A%2F%2Flocalhost%2Fexperiments%2FnewGALogin.php',
    'grant_type' => 'authorization_code'
)); 
Run Code Online (Sandbox Code Playgroud)

要么

curl_setopt($ch, CURLOPT_POSTFIELDS,
    'code=' . urlencode($code) . '&' .
    'client_id=' . urlencode($clientID) . '&' .
    'client_secret=' . urlencode($clientSecret) . '&' .
    'redirect_uri=http%3A%2F%2Flocalhost%2Fexperiments%2FnewGALogin.php' . '&' .
    'grant_type=authorization_code'
); 
Run Code Online (Sandbox Code Playgroud)