【问题标题】:Google OAuth 2 redirect_uri_mismatch - OmniAuth Rails appGoogle OAuth 2 redirect_uri_mismatch - OmniAuth Rails 应用
【发布时间】:2014-04-26 06:02:32
【问题描述】:

我在使用我的 Rails 应用验证 Google 帐户时遇到问题。 我在设计中使用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 是正确的,并且与请求的重定向 URI 相同,就像这样:

那么这里的主要问题是什么?

【问题讨论】:

  • 如何在代码中设置重定向 uri?您确定屏幕截图来自正确的项目和客户端 ID?
  • 是的,我确定。而且我没有在代码中手动设置重定向 uri,设计和omniauth 会自动为我完成,reidrect_url 参数是正确的!
  • 我将我的 redirect_uri 错误直接复制并粘贴到我的 google api redirect_uri 控制台中,并意识到我正在重定向到 127.0.0.1 而不是 localhost,因此请务必小心这些小细节。跨度>

标签: ruby-on-rails devise oauth-2.0 google-api omniauth


【解决方案1】:

试试这个方法:

add require "omniauth-google-oauth2" to devise.rb in config/initializers folder

add http://localhost:3000/users/auth/google_oauth2/callback 转为 Google API 控制台中的重定向 URL https://console.developers.google.com

重启服务器

【讨论】:

  • 但我必须添加gem 'omniauth-oauth2', '~> 1.3.1'以及"omniauth-google-oauth2"bundle install之前我需要做bundle update omniauth-oauth2
【解决方案2】:

请注意您设置的客户端 ID。
Google API 提供两种:
Google Compute 和 App Engine 的客户端 ID
Web 应用程序的客户端 ID

您需要将客户端 ID 用于 Web 应用程序

【讨论】:

    【解决方案3】:

    确保通过同意屏幕链接设置产品名称和电子邮件地址。

    根据您需要的 omniauth-google-oauth2 文档:

    转到“https://console.developers.google.com” 选择您的项目。 单击“API 和身份验证” 确保“Contacts API”和“Google+ API”已开启。 转到同意屏幕,并提供“产品名称” 等待 10 分钟让更改生效。

    我遇到了同样的问题,进行了更新,等了 10 分钟,仍然没有,去吃午饭,然后它开始工作。猜猜耐心是成功的关键之一。

    【讨论】:

      【解决方案4】:

      我的解决方案是在两个(代码/令牌)阶段强制 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

      【讨论】:

        猜你喜欢
        • 2016-01-26
        • 1970-01-01
        • 2012-07-14
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多