php - Google OAuth 2 - redirect_uri_mismatch 错误但 API 设置中的重定向 URI 是正确的

标签 php curl oauth-2.0

我一直在关注 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/

相关文章:

php - 我无法插入文本区域值

JavaScript 不更改 PHP 填充的文本字段

curl - 在Flutter中使用Dio进行POST时遇到问题

php - 在 PHP 上使用 curl 为 Web API 设置路径 HTTP GET 请求

java - 过滤或处理 OAuth2 中的 AuthenticationException

php - 如何修改此正则表达式以包含 https?

php - 使用 Doxygen 或 PHPDoc 记录获取/发布参数

shell - 变量没有在 jenkins 管道的 sh 中填充

java - Spring 社交 facebook 无法解码从 facebook 访问 token URI 发送的信息

qt - QtWebEngine 依赖应用程序的静态构建