Google Oauth2 giving "redirect_uri_mismatch" error in production when redirect uri setup correctly

Viewed 74

I have google oauth set up in a react project. It was working fine in development, locally. Once i promoted the oauth client to "production" and modified the JS origin and redirect URIs to production values, yet it gives this error

Error 400: redirect_uri_mismatch

You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy.

If you're the app developer, register the redirect URI in the Google Cloud Console. Request details: redirect_uri=http://super-server.herokuapp.com/v1/auth/google/callback Related developer documentation

These are the authorized redirect URIs within google cloud console:

As you can see, there are no authorized uri's with an HTTP schema. Theyre not even allowed in production mode. So im not sure where this is coming from, because the server is HTTPS

server:

enter image description here

Any advice?

3 Answers

the redirect uri must exactly match the one you are adding to Google developer console

If you check the error message your app is running with

http://super-server.herokuapp.com/v1/auth/google/callback

All the ones you have added are https

May i suggest fixing your app so that it runs https. I dont think that you will be able to add http as a production redirect uri endpoint.

documentation

Obtaining OAuth 2.0 access tokens

enter image description here

authorization-errors-redirect-uri-mismatch

enter image description here

Node.js

I dont know enough about react.js but with node you should be able to do something like this

const http = require('http');
const https = require('https');

In my case (MERN + passport.js), i had a configuration that looked like:

const googleOptions = {
  clientID: config.google.id,
  clientSecret: config.google.secret,
  passReqToCallback: true,
  callbackURL: '/v1/auth/google/callback',
  scope: ['profile', 'email']
};

Even though the server, the client && the configuration of the oauth client within google api console were all in production with everything setup for https, for some reason, the callbackURL kept firing with google oauth as http://my-domain.com/v1/auth/google/callback

so this fix may be hacky, but it did fix my oauth issues:

//Google Strategy
const googleOptions = {
  clientID: config.google.id,
  clientSecret: config.google.secret,
  passReqToCallback: true,
  callbackURL: config.environment == 'production' ? 'https://super-server.herokuapp.com/v1/auth/google/callback' : '/v1/auth/google/callback',
  scope: ['profile', 'email']
};
Related