Search code examples
ruby-on-railsdeviseomniauthomniauth-google-oauth2

google omniauth rails with devise authentication failure


I want add omniauth with Google on my Rails app but i have error after redirection

ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, redirect_uri_mismatch: Bad Request
{
  "error": "redirect_uri_mismatch",
  "error_description": "Bad Request"
}

I saw errors about bad redirection uri but not "bad request"

My redirection uri in google console

http://localhost:3000/clients/auth/google_oauth2/callback

My route

devise_for :clients, controllers: { omniauth_callbacks: 'clients/omniauth_callbacks' }

Gemfile

gem 'omniauth-oauth2'
gem 'omniauth-google-oauth2'
gem "omniauth-linkedin-oauth2"
gem 'omniauth-rails_csrf_protection', '~> 1.0'

devise

  config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], access_type: "online"

my link

<%= link_to "Sign in with Google", client_google_oauth2_omniauth_authorize_path, method: :post %>

Solution

  • Check your google developer account where you created the credentials. there should be a field named redirect URL where you should place the correct URL. This URL was used to redirect the user from modal opened on login with google.