PHP-Google OAuth 2-redirect_uri_mismatch错误,但API设置中的重定向URI正确
作者:互联网
我一直在https://developers.google.com/+/web/signin/server-side-flow处遵循该指南,直到将授权码发送到服务器为止.然后在PHP中,我尝试使用以下命令获取访问令牌:
$data = "code=".urlencode(trim($access_code)).
"&grant_type=authorization_code".
"&client_id=".urlencode($this->client_id).
"&client_secret=".urlencode($this->client_secret).
"&redirect_uri=".urlencode(trim($redirect_uri));
$curl = curl_init("https://accounts.google.com/o/oauth2/token");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($curl), true);
curl_close($curl);
return $result;
在PHP中,redirect_uri与google dev控制台中的重定向uris完全相同,但是调用始终返回:
array(1) { ["error"]=> string(21) "redirect_uri_mismatch" }
我究竟做错了什么?
供参考,这是我的Google API设置如下所示:
http://such-nom.com
http://www.such-nom.com
并且var通过$redirect_uri传递:
$redir = 'http://such-nom.com';
编辑:当在服务器端而不是通过google按钮生成请求令牌时,似乎完全一样的PHP可以工作.
解决方法:
事实证明,问题在于添加了斜杠并等待了几个小时.
因此,URL的完整列表如下所示:
http://such-nom.com
http://www.such-nom.com
http://such-nom.com/
http://www.such-nom.com/
标签:curl,oauth-2-0,php 来源: https://codeday.me/bug/20191121/2050855.html