Google OAuth2 Authorizing OAuth token error: redirect_uri_mismatch - oauth-2.0

I am Creating and Authorizing an OAuth Token according to this webpage: https://code.google.com/p/google-mail-oauth2-tools/wiki/OAuth2DotPyRunThrough
But I got this error: redirect_uri_mismatch.
The redirect URI in the request: urn:ietf:wg:oauth:2.0:oob did not match a registered redirect URI
from_login=1
cookie_policy_enforce=false
scope=https://mail.google.com/
response_type=code
access_type=online
redirect_uri=urn:ietf:wg:oauth:2.0:oob
as=-80019291b2cb8ed
display=page
pli=1
client_id=......
authuser=0
hl=en
I thought this might be helpful: Google OAuth 2 authorization - Error: redirect_uri_mismatch
But when I try to register the redirect url to my console, I was told that the url is invalid.

The redirect_uri (urn:ietf:wg:oauth:2.0:oob) is only applicable to those Google client ids that have been generated for installed applications. You can go to your console and create a new client id of this type.

Just in case if you're using Google+ javascript button (with web application), you have to put postmessage instead of actual URI. It takes me almost whole day to figure out this, because Google docs doesn't clearly stand it for some reason.

For my web application i corrected my mistake by writing
instead of : http://localhost:11472/authorize/
type : http://localhost/authorize/

When you register your app at https://code.google.com/apis/console and
make a Client ID, you get a chance to specify one or more redirect
URIs. The value of the redirect_uri parameter on your auth URI has to
match one of them exactly.

Please note that the 'redirect_uri' value of the Token request need to be the same as the 'redirect_uri' value of the Authorization request.

Lost 4 or 5 hours with this... use 'postmessage' as parameter value, not the real Redirect Uri...
$client->setRedirectUri('postmessage');

For anybody that is still stumped with this problem, you must have the 'Platform' set to 'Native (Windows Mobile, Blackberry, desktop, devices, and more)' when registering your app in the Google Cloud Console, otherwise, it will not let you use 'urn:ietf:wg:oauth:2.0:oob' as the redirect URI.

In my case, instead of creating web app, i just chose Other in:
OAuth Client ID > Other
and thats it.

2015July15 - working signin started causing Error 400 with Error: redirect_uri_mismatch
i posted a solution on a similar SO QUESTION: changed loading script to
<script src="https://apis.google.com/js/client:platform.js?onload=startApp></script>

For my native app, I tried dozens of different things. I finally got it to work by using "redirect_uri=http%3A%2F%2Flocalhost%3A1234" where 1234 is the port number. This has to be identical in the two requests (authorization code and the access token). Notice the use of percent encoding.

I was getting this error, because I was incorrectly following the steps for installed application flow here
https://github.com/googleads/googleads-python-lib/wiki
instead of the server to server flow.

The easiest way is to create the service account.
Create the document using your own account and share it with the service account.

redirect_uri must be an EXACT MATCH on the developers console.
In my case, this was due to a trailing slash( / ).
In the Google Cloud console, I had http://localhost:8080 under the redirect URIs in the list while my code was sending http://localhost:8080/ while making the oAuth call.

Related

Issue with invalid_grant error with Google Oauth2

Cheers everybody,
we have been deeply reading google documentation on exchanging access_token from google in order our (delphi)desktop application to SSO with google from server side. Here is the payload we send first look like:
https://accounts.google.com/o/oauth2/v2/auth?client_id=1000217514248-t1lojs6f8ed7l9ocrpbm98leahtum8n1.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&state=E1DF2FBA-0A66-4D69-B594-5EB8F7828AF7&scope=openid+profile&include_granted_scopes=true&code_challenge=C832DA50-E55A-499D-89B8-493BB4123C94&login_hint=test#Speelkriebel.be
Normally after this it redirects me to login in to our test user and after this according to the documentation we send a POST request to the end point token in order to get the access_token and refresh_token...: 'https://oauth2.googleapis.com/token
with the following parameters, the 'code' is generated we also send it as follow:
client_id=1000217514248-t1lojs6f8ed7l9ocrpbm98leahtum8n1.apps.googleusercontent.com
grant_type=authorization_code
client_secret=******
code= 4/1AY0e-g4GlavO38PI5Oo3vq04Pc4lMWN77et-02UiVWOsT-IyRQnU1lq19qo
redirect_uri = urn:ietf:wg:oauth:2.0:oob
The response is always
{
"error_description": "Missing code verifier.",
"error": "invalid_grant"
}
We have tried to send the client secret id also, Does it have to do with our code_challenge ? are the end points url and initial url okay? What are we missing? We are using CEF4Delphi as "browser like experience in order for the user to type in their google credentials. We have been reading this: https://developers.google.com/identity/protocols/oauth2/web-server#offline
We were also trying the playground :https://developers.google.com/oauthplayground/
we were sending the initial url in a chrome which generated a "code" and in the playground we inserted the code, and still got the same error of missing code verifier.
Thanks Guys
You seam to have URL encoded a lot of the values try not doing that. Also try using the basic call, before you start adding everything else. It should help you figure out which one of those extra parameters you are sending that's causing your issues.
https://accounts.google.com/o/oauth2/auth?client_id={clientid}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=profile&response_type=code
Also make sure that the client id is from an installed / other type client
This may also help Google 3 Legged OAuth2 Flow
For installed apps, the code challenge and verifier are parameters for enhancing the security of the OAuth flow through PKCE [1].
There is additional documentation about generating a code challenge and verifier here [2].
[1] https://www.rfc-editor.org/rfc/rfc7636
[2] https://developers.google.com/identity/protocols/oauth2/native-app#step1-code-verifier

How can I edit existing Authorized redirect uri in google console?

I created an oAuth key for my laravel site for google login. I successfully implemented this in local. But when I migrated my site to live I got this error:
400. That’s an error.
Error: redirect_uri_mismatch
Request Details
client_id=xxxxxxx-0fic8dnvcgo672unju9ai619iXXXXXX.apps.googleusercontent.com
redirect_uri=https://laravel.themenepal.com/gharsansar/auth/google/callback
scope=openid profile email
response_type=code
state=NRu0eU9UdwEXYSV0dixOc0BjuH3bluFkHiBDJFgO
That’s all we know.
I think this is primarily due to redirect url being mismatched with what is set in Google console. I've changed the redirect urls in .env. I have searchd a lot to be able to update the redirect uri in google console but nothing helped.
If you know that would be a great help.
UPDATE: The screenshot of the url edit page is as follows:
If you goto: https://console.developers.google.com/apis/credentials and select the correct project, then the correct the correct OAuth 2.0 client IDs and edit the correct OAuth 2.0 client IDs.
There appears to be no option to edit it if it's created as an OAuth 2.0 client ID of type "Other." If you choose Web Application, you can edit them yourself.

Invalid Redirect URI Deezer OAuth

I am getting the Invalid Redirect URI while trying to authenticate. This is the first step to authenticating on the guide and I have literally copy-pasted the code into my browser and subbed in my Application Domain and Application ID and it says Please enter a valid redirect_URI. https://connect.deezer.com/oauth/auth.php?app_id=MyAppID&redirect_uri=MyRedirectURI
I had the same problem until I found out that unlike any other music streaming service, Deezer does not allow (or it's a bug) redirect uris that
contain a port like localhost:9000/deezer/callback
have a subdomain like cheapuri.noip.me
I guess co.uk then won't work either
It seems that the only thing that is working are "normal" uris like api.com.
The way I work around this when developing:
Let Deezer redirect you to some non existing uri, then change the uri in the browser bar.
I would double check that the redirect_uri in your /oauth request URL matches exactly that which is referenced in your oauth configuration/whitelist settings for Deezer.
Even the smallest difference like http vs https or including wwww., etc. can throw an error. You might find some of the tips in this OAuth Troubleshooting Guide helpful for other common OAuth errors to check, too!
The first item in that guide covers some common invalid redirect errors.

Google OAuth 2.0 redirect_uri_mismatch error

I created a Google OAuth 2.0 ClientID and secret in Google Developer console
After that I tested in Google OAuth playground (https://developers.google.com/oauthplayground).
and registered ClientID and secret already created above and applied to Google OAuth 2.0 playground setting menu.
Some people say that after creating ClientID/secret they need some time for testing. So after two days I tried to test in the same conditions but the error is same redirect_uri_mismatch.
How can I solve this?
As little as having a '/' at the end of your uri and not having the same '/' at the end in your code will throw it off.
Your site URL and the Authorized redirect URIs in developer console should be the exact match.
This kind of error occurs if one URL has www (http://www.example.com) and the other URL is non-www (http://example.com).
Other common URI mismatch are:
Using http:// in Authorized Redirect URIs and https:// as actual URL, or vice-versa
Using trailing slash (http://example.com/) in Authorized Redirect URIs and not using trailing slash (http://example.com) as actual URL, or vice-versa
Here is the step-by-step procedure (with screenshots) to update the Authorized redirect URIs in Google Developer Console (For those like me who found it difficult to get to that page).
Go to https://console.developers.google.com
Select your Project
Click on the menu icon
Click on API Manager menu
Click on Credentials menu. And under OAuth 2.0 Client IDs, you will find your client name. In my case, it is Web Client 1. Click on it and a popup will appear where you can edit Authorized Javascript Origin and Authorized redirect URIs.
Here is a Google article on creating project and client ID.
It should be a exact match what you have given in the console.developers.com.
In my case I missed the www in the url.
For eg: you have given http://www.google.com but in console.developers.com you gave http://google.com
It will still throw error. So it should be exact match.
The redirect URI (where the OAuth response is returned to) has to be registered in Google APIs console, and the error is indicating that you haven't done that, or haven't done it correctly.
Go to the console for your project and look under API Access. You should see your client ID & secret there, along with a list of redirect URIs. If the URI you want isn't listed, click edit settings and add the URI to the list.
I kept getting this same error until I realized that I needed to put "signin-google" at the end of the redirect setting in the Google API console, like this (ie, NOT http://www.example.org/api):
http://www.example.org/api/signin-google
(Magento 1.*) if You use inchoo Social Connect Magento extension then:
Set below url in your google app (OAuth 2.0 client IDs):
Authorized Redirect URIs: http://www.example.com/socialconnect/google/connect/
Authorized JavaScript Origins: http://www.example.com
Don’t forget to replace http://www.example.com with your domain
Please make sure that in your google-client-api, the value of credentials in these field are matched as what you got from Google API console:
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://example.com/oauth2callback');
$client->setDeveloperKey('xx');
This could happen when the value of setRedirectUri is different from the one you set in Google API console.

Google OAuth 2 authorization - swapping code for token

I'm trying to implement Google OAuth 2 to get access to Google APIs. I follow this guide, using server-side scenario.
I have no problem with getting the code, server redirects to localhost (which is the only server allowed in redirect URIs for now).
To achieve this, I go to https://accounts.google.com/o/oauth2/auth?client_id=whatever.apps.googleusercontent.com&redirect_uri=http://localhost/&scope=https://www.google.com/m8/feeds/&response_type=code page.
Then, I tried using curl (as in guide) to test, if Google's server responds with access token. However, it seems to fail very hard. Only response I can get is {"error":"invalid_client"}. I'm sure I provide everything Google wants me to provide - code, client ID, client secret, redirect URI (localhost) and grant_type=authorization_code.
Whole curl command line is:
curl https://accounts.google.com/o/oauth2/token -d "code=<code>&client_id=whatever.apps.googleusercontent.com&client_secret=<won't tell!>&redirect_uri=http://localhost&grant_type=authorization_code"
Am I missing something? How can I exchange code for access token?
Did you urlencode your client secret and redirect url? That works for me.
Should be http%3A%2F%2Flocalhost instead of https://localhost.
I had the same error until I realized that I was trying to connect with a client ID which I created for iOS. So for me the solution was to create a new API key-secret pair on the API Console with the platform set to "other".
Now I'm getting other errors but that's another story. ;)
Happens when you use wrong Client secret. Make sure you are using correct Client secret from Google API console. I was using Email address since API console displays the information in the following order:
Client ID
Email address
Client secret

Resources