我一直在关注 https://developers.google.com/+/web/signin/server-side-flow 的指南到我有一个授权码发送到我的服务器的地步。然后在 PHP 中,我尝试使用以下方法获取访问 token :
$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 开发控制台中的重定向 uri 完全相同,但调用总是返回:
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';
编辑:当请求 token 是在服务器端而不是通过 google 按钮生成时,PHP 的工作原理似乎完全相同。
最佳答案
原来问题是添加尾部斜杠并等待几个小时。 因此,完整的 URL 列表如下所示:
http://such-nom.com
http://www.such-nom.com
http://such-nom.com/
http://www.such-nom.com/
关于php - Google OAuth 2 - redirect_uri_mismatch 错误但 API 设置中的重定向 URI 是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25594672/