17

The URL's seem right (Last updated them yesterday):

enter image description here

The files too:

omniauth.rb:

provider :google_oauth2, 'MY_CLIENT_ID.apps.googleusercontent.com', 'MY_CLIENT_SECRET',
           :scope => 'https://mail.google.com/mail/feed/atom/'

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

header.html.erb

<li><%= link_to "Sign in with Google", "auth/google_oauth2" %></li>

routes.rb:

match '/auth/:provider/callback', to: 'sessions#omniauth_create'

But I'm getting this:

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

(Twitter and Facebook OmniAuth are working perfectly)

Not sure what is the problem. Any usggestions to fix this?

EDIT

I changed the URI to http...:

enter image description here

But still getting the same error.

alexchenco
  • 53,565
  • 76
  • 241
  • 413

5 Answers5

22

It looks like the request is hitting http://localhost:3000/auth/google_oauth2/callback, but your specified redirect URI matching the similar pattern is for https. Adding http://localhost:3000/auth/google_oauth2/callback to your list of redirects may potentially solve that issue.

EDIT: Another potential fix is including a trailing / in the corresponding redirect URIs, which appeared to work in this case.

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • Thanks. I thought was that too. I added a `http://localhost:3000` redirect and updated omniath.rb. But still getting the same error (please see my **EDIT**). – alexchenco Nov 03 '12 at 01:27
  • 5
    @alexchenco Ah, I see. Have you tried adding both `http` and `https` to the URI list? I agree that it looks like you're doing everything right, but I have usually defaulted to adding a both `http` and `https` URIs whenever OAuth is involved. Also, long shot, but have you tried adding trailing `/`'s to the URIs? So potentially four URIs (overkill, yes :) ): `http` and `https`, one each including the trailing frontslash and without it. – RocketDonkey Nov 03 '12 at 01:32
  • Thanks a lot! Add to add a trailing `/`. Very strange, my Rails app doesn't have a trailing `/`, and the example Google provides doesn't include it either. – alexchenco Nov 03 '12 at 01:46
  • @alexchenco Awesome! I don't even want to try to theorize on why because I'll be wrong, but I have run across that same problem more than once, so I default to throwing all possible combos in there. One of these days I'll get more efficient about it :) – RocketDonkey Nov 03 '12 at 01:47
  • Adding `?close` to the end of redirect uris helped me. – Vlad Holubiev Jan 01 '15 at 18:18
  • Wow, soooo much black magic here O.o And nothing worked for me :P – bbozo Nov 02 '15 at 15:57
4

There's a relatively fresh issue with omniauth-oauth2 gem version 1.4 https://github.com/intridea/omniauth-oauth2/issues/81#issuecomment-151038559

Temporary fix is to downgrade that gem explicitly in the Gemfile

gem 'omniauth-oauth2', '~> 1.3.1'
bbozo
  • 7,075
  • 3
  • 30
  • 56
  • 2
    they saying to avoid "thank" so i'm gonna say "love you" for such a simple solution :* – Mani Nov 02 '15 at 08:37
0

enter image description here

foo

vi config/initializers/omniauth.rb OmniAuth.config.full_host = 'https://localhost:3000' Rails.application.config.middleware.use OmniAuth::Builder do provider :google_oauth2, 'google_client_id', 'google_client_secret' end

cgg5207
  • 75
  • 2
0

Sharing a very simple copy-paste solution that worked for me.

I copied whatever I was specifying in my code as redirectUri i.e. "redirect_uri": "http://127.0.0.1:3001/" and pasted the value of this key inside the Google settings that ask for Authorized redirect URIs. This way I'm ensured that both the parameters are same.

If the url was computed, I would console.log() it and copy it from the console window before pasting it in for google settings.

Aakash
  • 21,375
  • 7
  • 100
  • 81
0

I tried all of the above but didn't work for me. In the end noticed in my error message my call back was slightly different. I had a users between localhost:3000 and auth. Not really sure why.

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

Changed it, waited 30 mins and it worked.

R. Lee
  • 3
  • 2