douchuanchai2793 2014-08-31 17:51
浏览 196
已采纳

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

I've been following the guide at https://developers.google.com/+/web/signin/server-side-flow to the point where I have an authorization code sent to my server. Then in PHP I attempt to get the access tokens using the following:

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

The redirect_uri is EXACTLY the same in PHP as it is in the redirect uris in the google dev console, but the call always returns:

array(1) { ["error"]=> string(21) "redirect_uri_mismatch" }

What am I doing wrong?

For reference, this is what my google API setting looks like:

http://such-nom.com
http://www.such-nom.com

And the var passed as $redirect_uri:

$redir = 'http://such-nom.com';

Edit: It seems the exact same PHP works when the request token is generated serverside rather than through the google button.

  • 写回答

1条回答 默认 最新

  • douwu8060 2015-04-07 14:39
    关注

    Turns out the issue was adding a trailing slash and waiting a couple of hours. So the full list of URLs would look like:

    http://such-nom.com
    http://www.such-nom.com
    http://such-nom.com/
    http://www.such-nom.com/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿