Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google OAuth 2 redirect_uri_mismatch - OmniAuth Rails app

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?

like image 273
Azzurrio Avatar asked Mar 19 '14 18:03

Azzurrio


2 Answers

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

like image 78
Luan D Avatar answered Sep 19 '22 01:09

Luan D


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

like image 23
Kodak Avatar answered Sep 22 '22 01:09

Kodak