Google OAuth 2 redirect_uri_mismatch – OmniAuth Rails应用
我在使用Rails应用validationGoogle帐户时遇到问题。 我正在使用带有Devise的omniauth-google-oauth2 gem 。 我尝试访问Google帐户时始终收到此错误消息:
Error: redirect_uri_mismatch The redirect URI in the request: http://localhost:3000/users/auth/google_oauth2/callback did not match a registered redirect URI
我确信我的Google控制台应用中注册的重定向URI是正确的,与请求的相同,就像这样:
那么这里的主要问题是什么?
试试这种方式:
add require "omniauth-google-oauth2" to devise.rb in config/initializers folder
add http://localhost:3000/users/auth/google_oauth2/callback
到google API控制台中的重定向urlhttps://console.developers.google.com
重启服务器
我的解决方案是在两个(代码/令牌)阶段强制redirect_url,devise.rb初始化器:
CALLBACK_URL = 'https://SOMESERVER/users/auth/google_oauth2/callback' Devise.setup do |config| config.omniauth :google_oauth2, "SOMECLIENTID.apps.googleusercontent.com", "SOMEKEY", { :client_options => {:ssl => {:ca_file => 'C:\Ruby21\cacert.pem'}}, :provider_ignores_state => true, :prompt => "select_account", :redirect_uri => CALLBACK_URL, setup: (lambda do |env| request = Rack::Request.new(env) env['omniauth.strategy'].options['token_params'] = {:redirect_uri => CALLBACK_URL} end) } end
这里有关于这个问题的讨论: https : //github.com/zquestz/omniauth-google-oauth2/issues/181
请注意您设置的客户端ID。
Google API提供了两个:
Google Compute和App Engine的客户端ID
Web应用程序的客户端ID
您需要将Web客户端ID用于Web应用程序
确保通过Consent Screen Link设置产品名称和电子邮件地址。
根据omniauth-google-oauth2文档,您需要:
转到“ https://console.developers.google.com ”选择您的项目。 点击“API&auth”确保“通讯录API”和“Google+ API”已启用。 转到同意屏幕,并提供“产品名称”等待10分钟以使更改生效。
我有同样的问题,做了更新,等了10分钟,仍然没有,去吃午饭,然后它开始工作。 猜猜耐心是这一方面取得成功的关键。
- 如何使用带有Ruby Google API客户端的Gmail API发送消息?
- Google-api-client要求访问令牌
- Rails中的plusAPmains.circles.addPeople的Google API生成403 Forbidden
- 为什么Google API无论输入设置了+01偏移量?
- 如何从服务帐户谷歌驱动器api下载文件?
- 通过Gmail API创建包含收件人的Gmail草稿
- 尝试使用YoutubeV3 API创建实时广播时出现“需要登录”错误
- 访问Google Plus联系人google-api-ruby-client和omniauth-google-oauth2
- 如何使用OAuth 2.0为Ruby中的google预测API进行授权?