gpt4 book ai didi

php - 谷歌 API OAuth 2.0 CURL 返回 "Required parameter is missing: grant_type"

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:06:36 27 4
gpt4 key购买 nike

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

我可以从 Google 获取代码,但是当我发回此代码以尝试获取访问 token 时,它总是给我错误“缺少必需的参数:grant_type。错误 400”,即使 grant_type 存在。

此外,如果我将内容长度指定为 0 以外的任何值,它会引发其他错误。

这是执行此 curl 帖子的代码:

$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'
));

最佳答案

尝试

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'
));

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'
);

关于php - 谷歌 API OAuth 2.0 CURL 返回 "Required parameter is missing: grant_type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8198072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com