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

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/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?