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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。