10

I've a problem with authenticating Google account with my Rails app. I'm using omniauth-google-oauth2 gem with Devise. Always get this Error when I try to access google account:

Error: redirect_uri_mismatch
The redirect URI in the request: http://localhost:3000/users/auth/google_oauth2/callback did not match a registered redirect URI

I'm sure that the registered redirect URI in my google console app is right and identical with requested one, just like that:

enter image description here

so what's main the problem here?

Community
  • 1
  • 1
Azzurrio
  • 1,330
  • 1
  • 17
  • 35
  • how are you setting the redirect uri in the code? are you sure that screenshot is from the correct project and client id? – Vinicius Braz Pinto Mar 19 '14 at 19:21
  • yeah, i'm sure. and i don't set the redirect uri in the code manually, devise and omniauth do it automatically for me and the reidrect_url param is right! – Azzurrio Mar 19 '14 at 20:03
  • I copy and pasted my redirect_uri error directly into my google api redirect_uri console, and realized I was redirecting to 127.0.0.1 as opposed to localhost, so make sure you are careful with such small details. – Danny May 10 '16 at 17:28

4 Answers4

12

try this way :

add require "omniauth-google-oauth2" to devise.rb in config/initializers folder

add http://localhost:3000/users/auth/google_oauth2/callback into Redirect URL in google API console https://console.developers.google.com

restart server

Luan D
  • 1,320
  • 1
  • 13
  • 26
  • but I have to add `gem 'omniauth-oauth2', '~> 1.3.1'` as well along with `"omniauth-google-oauth2"` and before `bundle install` I need to do `bundle update omniauth-oauth2` – Mani Nov 02 '15 at 08:50
3

Mine solution is to force redirect_url in both (code/token) stages, devise.rb initializer:

CALLBACK_URL = 'https://SOMESERVER/users/auth/google_oauth2/callback'

Devise.setup do |config|
  config.omniauth :google_oauth2,
    "SOMECLIENTID.apps.googleusercontent.com",
    "SOMEKEY",
    {
    :client_options => {:ssl => {:ca_file => 'C:\Ruby21\cacert.pem'}},
    :provider_ignores_state => true,
    :prompt => "select_account",
    :redirect_uri => CALLBACK_URL,
    setup: (lambda do |env|
      request = Rack::Request.new(env)
      env['omniauth.strategy'].options['token_params'] = {:redirect_uri => CALLBACK_URL}
    end)
    }
end

there is discussion about the issue here: https://github.com/zquestz/omniauth-google-oauth2/issues/181

Kodak
  • 1,779
  • 2
  • 18
  • 29
0

Be careful of what client id you are setting.
Google API provides two:
Client ID for Google Compute and App Engine
Client ID for web applications

You need to use Client ID for web applications

-1

Make sure you set up the Product Name and Email address via the Consent Screen Link.

Per the omniauth-google-oauth2 documentation you need to:

Go to 'https://console.developers.google.com' Select your project. Click 'APIs & auth' Make sure "Contacts API" and "Google+ API" are on. Go to Consent Screen, and provide a 'PRODUCT NAME' Wait 10 minutes for changes to take effect.

I had the same issue, made the updates, waited 10 minutes, still nothing, went to lunch, then it started to work. Guess patience was part of the key to success on this one.

Steve
  • 2,596
  • 26
  • 33