3

I configured Devise to use omniauth with Facebook and Google. It works fine with Facebook but I face a problem with Google.

I get this error again and again:

ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, redirect_uri_mismatch:

{"error" : "redirect_uri_mismatch"}

[localhost] [127.0.0.1] [6a9377fe-d0b3-42] RuntimeError - Could not find a valid mapping for path "/users/auth/google_oauth2/callback":

devise (3.5.2) lib/devise/mapping.rb:49:in `find_by_path!'

I tried several URIs in the Google console but anyone didn't seem to work:

Google Console

Gemfile

# Social Networks Authentification
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'

devise.rb

config.omniauth :facebook, ENV['OAUTH_FACEBOOK_ID'], ENV['OAUTH_FACEBOOK_SECRET'],
                       scope: 'public_profile', image_size: {height: 1600}, info_fields: 'name, id, first_name, last_name, gender, hometown, cover, email, link' # list of permissions

# Not working, "error" : "redirect_uri_mismatch"
config.omniauth :google_oauth2, ENV['OAUTH_GOOGLE_ID'], ENV['OAUTH_GOOGLE_SECRET']

omniauth_callbacks_controller.rb

def self.provides_callback_for(provider)
    class_eval %Q{
      def #{provider}
        @user = User.find_for_oauth(env["omniauth.auth"], current_user)

        if @user.persisted?
          handle_redirect("devise.#{provider}_uid", "#{provider}".capitalize)
        else
          session["devise.#{provider}_data"] = env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end
    }
  end

  [:facebook, :google_oauth2].each do |provider|
    provides_callback_for provider
  end

  def handle_redirect(_session_variable, kind)
    # here we force the locale to the session locale so it siwtches to the correct locale
    I18n.locale = session[:omniauth_login_locale] || I18n.default_locale
    sign_in_and_redirect @user, event: :authentication
    set_flash_message(:notice, :success, kind: kind) if is_navigational_format?
  end

omniauth_controller.rb

class OmniauthController < ApplicationController
  def localized
    # Just save the current locale in the session and redirect to the unscoped path as before
    session[:omniauth_login_locale] = I18n.locale
    redirect_to user_omniauth_authorize_path(params[:provider])
  end
end

routes.rb

  devise_for :users, skip: [:session, :password, :registration, :confirmation], controllers: { omniauth_callbacks: 'omniauth_callbacks' }


  localized do

    get 'auth/:provider' => 'omniauth#localized', as: :localized_omniauth

    devise_for :users, :path => 'accounts', skip: :omniauth_callbacks, :controllers => {sessions: 'sessions', registrations: 'registrations', passwords: 'passwords'}

end

user.rb

devise :omniauthable, :omniauth_providers => [:facebook, :google_oauth2]

I have the issue both in local and production.

Can anyone help me please? I really don't know what else I can do.

Lahiru Jayaratne
  • 1,684
  • 4
  • 31
  • 35
  • Could you try using `http://127.0.0.1:3000/[PATH]` and run your app with that address – Joseph N. Nov 19 '15 at 15:36
  • Still not working... :-( But this time I have this: https://www.dropbox.com/s/3wok8pb7oob03ou/Capture%20d%E2%80%99%C3%A9cran%202015-11-19%20%C3%A0%2016.39.48.png?dl=0 – Céline Martinet Sanchez Nov 19 '15 at 15:41
  • The first time the error was on my console directly – Céline Martinet Sanchez Nov 19 '15 at 15:42
  • Well, I saw in [this post](http://stackoverflow.com/questions/10215863/why-is-google-oauth-returning-invalid-redirect-uri-in-my-rails-app) that I should not user localhost. So I tried to use ngrok but it didn't change a thing. [Picture 1](https://www.dropbox.com/s/l5056djx5j9cyae/Capture%20d%E2%80%99%C3%A9cran%202015-11-20%20%C3%A0%2007.58.29.png?dl=0) [Picture 2](https://www.dropbox.com/s/4ldyuj63l04s4h0/Capture%20d%E2%80%99%C3%A9cran%202015-11-20%20%C3%A0%2007.56.22.png?dl=0) – Céline Martinet Sanchez Nov 20 '15 at 06:56

3 Answers3

6

Allright, finally found out what was the problem...

The 'omniauth-google-oauth2'gem only works with version 1.3.1 of the 'omniauth-oauth2' gem.

So I changed my Gemfile like this:

# Social Networks Authentification
gem 'omniauth-oauth2', '~> 1.3.1' # Don't touch that unless you don't want Google omniauth to work!
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'omniauth-linkedin'
gem 'omniauth-google-oauth2' # Works only with omniauth-oauth2 v 1.3.1

And now everything works fine!

See this thread for more details.

  • Okay, so it appears that this is still an issue in 2019, with the added complication that v1.3.1 uses hard-coded Google Plus APIs that have been turned off. Investigating, – Ben Hull May 08 '19 at 00:33
  • @Beejamin What were the results of your investigation? I'm currently facing this same issue. – Michael Hays Jul 10 '19 at 20:56
  • @MichaelHays, I am using `'omniauth-oauth2', '~> 1.6'` and `'omniauth-google-oauth2', '~> 0.6.1'` and that's working. I can't remember if there were other steps - message back if that doesn't fix and I'll have a dig! – Ben Hull Jul 11 '19 at 02:15
  • 1
    @Beejamin thanks for the offer! I ended up going with a different approach, which I'm trying to add to `omniauth-google-oauth2` as documentation: https://github.com/zquestz/omniauth-google-oauth2/issues/365 – Michael Hays Jul 12 '19 at 15:12
1

Please check following steps to resolve issue

Step1. Error: redirect_uri_mismatch as the following error, copy the following link text The redirect URI in the request, http://yourlinks:3000/, does not match the ones authorized for the OAuth client. Visit...

Step2. Login into Google Console Api, go to Credential, Choose your Api and past the above link to Authorized redirect URIs section

Step3. Save and test again

Thank you.

Hai Dinh
  • 1,499
  • 2
  • 17
  • 30
1

I had the same issue and i made a plenty of mistakes, which i corrected by following these steps:

  1. First check whether the redirect url in the google Credentials is correct,
  2. Then check the version of "omniauth-google-oauth2", changing version of this gem worked for me.

Gemfile contains

gem "gmail"
gem "omniauth-google-oauth2"
Somesh Sharma
  • 529
  • 5
  • 7