Google OAuth 2 - redirect_uri_mismatch error but redirect URI in API settings

I followed the tutorial at https://developers.google.com/+/web/signin/server-side-flow until the point where I have an authorization code sent to my server. Then in PHP I am trying to get 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;

      

Redirect_uri is EXACTLY the same in PHP as uris redirect in 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 looks like:

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

      

And var is passed as $ redirect_uri:

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

      

Edit: It looks like the same PHP works when the request token is generated by the server and not via the google button.

+3


source to share


1 answer


It turns out the problem was adding a trailing slash and waiting a couple of hours. So the complete list of URLs would look like this:



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

      

+3


source







All Articles