0

I'm getting this error: OAuth2::Error redirect_uri_mismatch

This is how I have omniauth.rb set up, per omniauth-google-oauth2's documentation.

# config/initializers/omniauth.rb
OmniAuth.config.logger = Rails.logger
OmniAuth.config.full_host = Rails.env.production? ? 'https://www.********.org' : 'http://localhost:3000'

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_OAUTH_CLIENT_ID'], ENV['FACEBOOK_OAUTH_SECRET']
  provider :google_oauth2, ENV['GOOGLE_OAUTH_CLIENT_ID'], ENV['GOOGLE_OAUTH_SECRET'], {scope: 'profile', image_aspect_ratio: 'square', image_size: 48, access_type: 'online'}
end

And this is how I have my routes.rb set up to handle the callback:

# config/routes.rb
  get 'auth/:provider/callback', to: 'sessions#create'

Here's a screenshot of my Google Developer Console, where I have my authorized redirect uri set up as http://localhost:3000/auth/google_oauth2/callback.

I have tried it as just http://localhost:3000 and with https and http://localhost/auth/google_oauth2/callback and several other variations.

Some people say the console can take a while to update, so I wonder if it's possible I tried the correct thing at some point and didn't give it enough time (more than 5 minutes?) to update.

Facebook's callback (http://localhost:3000/auth/facebook/callback at the Facebook developer console) works, taking me all the way through to the SessionsController's #create action, where I haven't added a way to handle it yet, so it's throwing an error there.

Can anyone spot what I'm doing wrong!?

ctrutmann
  • 31
  • 5
  • I know this has been asked a lot on SO and I apologize for posting it again, but I've tried so many suggested answers and got desperate enough I've posted my own situation. Thanks! – ctrutmann Oct 05 '16 at 20:17
  • I solved this. I'm not using Devise, I've built the user authentication system following along in Michael Hartl's tutorial, and omniauth-google-oauth2 is expecting Devise to handle the callback. To customize it without Devise, I finally found [this article](https://rileymjones.wordpress.com/2015/11/24/omniauth/), which added a whole setup to the omniauth.rb: `setup: (lambda do |env| request = Rack::Request.new(env) env['omniauth.strategy'].options['token_params'] = {:redirect_uri => 'http://127.0.0.1:3000/auth/google_oauth2/callback'} end)` – ctrutmann Oct 05 '16 at 22:04
  • Try this answer it worked with me https://stackoverflow.com/a/41466634/1770571 – Salma Gomaa Jan 16 '19 at 11:21

0 Answers0