Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google OAuth 2.0 "error" : "redirect_uri_mismatch"

I spend already one day, crashed one glass and I am really angry about it, I do not understand what google want from me, and what is wrong.

I've enabled Google+ Api in developers console google_ api enabled , created new OAuth Client ID client id

    $ch = curl_init('https://accounts.google.com/o/oauth2/token');
curl_setopt($ch,CURLOPT_POSTFIELDS,'code=4%2FPp1GWqC6PIr3wNYrZ5kK4T9oitxBoo0fBqBrVNQfE-g.ElKDUjQ7E28SoiIBeO6P2m-0RPaolgI&grant_type=authorization_code&redirect_uri=https%3A%2F%2Fmyprivatedomain.local.com%2Foauth2callback&client_id=%mycliet_id%&client_secret=%mysecret%');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
var_dump(curl_exec($ch));

created all like in instructions here: https://developers.google.com/+/web/signin/server-side-flow, gplus button appear on page, and it successfully request access for authorized user. but when I made step 8 Step 8: Initialize the Google API client library and start the Google+ service my request every time get response "error" : "redirect_uri_mismatch"

I know, that this error appear when you do not registered redirect_uri in Google Console, or when you make a type mistake in it, but I registered it, and also just for testing tried to setup different urls (changed domain names, changed protocols from https to https), but it never working! I have no idea what else I can check, please advice at least something.

like image 517
Unstaged Avatar asked Feb 04 '15 12:02

Unstaged


People also ask

How do I fix OAuth error?

When a user tries to login after the session id is expired, the system throws the OAuth error. Solution: Typically, clearing the browser or device cache fixes the problem.

What is redirect URI in oauth2?

A redirect URI, or reply URL, is the location where the authorization server sends the user once the app has been successfully authorized and granted an authorization code or access token.


2 Answers

The docs say in Step 1. https://developers.google.com/+/web/signin/server-side-flow#step_1_create_a_client_id_and_client_secret that there must be no redirect URIs configured, only "Authorized JavaScript origins". In the authorization request and the token exchange, the redirect_uri parameter value should be set to postmessage.

Edit: Prior art on this: Google OAuth 2 authorization - Error: redirect_uri_mismatch

like image 151
Hans Z. Avatar answered Oct 06 '22 10:10

Hans Z.


Just ran into this problem myself. In my case, my credentials were set up for an installed application, NOT a web application. It seems that Installed applications cannot be configured with redirect URLs. I created a new credential as a web application, and this gave me the option to set a series of redirect urls.

Following the advice of this and other answers, I made sure the URL's matched (copy-paste) and this functioned correctly for me. I also did this in an Incognito Window.

The result was my browser being forwarded to the URL I put in the redirect_url parameter with a special query string parameter code populated with the code to use for the next step.

like image 45
Ben Avatar answered Oct 06 '22 11:10

Ben