Getting “Error: redirect_uri_mismatch” with google_oauth2

瘦欲@ 提交于 2019-12-20 10:30:47

问题


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

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...:

But still getting the same error.


回答1:


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.




回答2:


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'



回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/13204474/getting-error-redirect-uri-mismatch-with-google-oauth2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!