【问题标题】:Google OAuth 2.0 and Devise - "error" : "redirect_uri_mismatch"Google OAuth 2.0 和设计 - “错误”:“redirect_uri_mismatch”
【发布时间】:2016-02-21 19:45:43
【问题描述】:

我将Devise 配置为将omniauthFacebookGoogle 一起使用。它适用于Facebook,但我遇到了Google 的问题。

我一次又一次地收到这个错误:

错误——omniauth: (google_oauth2) 身份验证失败! invalid_credentials:OAuth2::Error,redirect_uri_mismatch:

{“错误”:“redirect_uri_mismatch”}

[本地主机] [127.0.0.1] [6a9377fe-d0b3-42] RuntimeError - 找不到路径“/users/auth/google_oauth2/callback”的有效映射:

设计 (3.5.2) lib/devise/mapping.rb:49:in `find_by_path!'

我在Google 控制台中尝试了几个 URI,但似乎都没有工作:

宝石文件

# Social Networks Authentification
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'

设计.rb

config.omniauth :facebook, ENV['OAUTH_FACEBOOK_ID'], ENV['OAUTH_FACEBOOK_SECRET'],
                       scope: 'public_profile', image_size: {height: 1600}, info_fields: 'name, id, first_name, last_name, gender, hometown, cover, email, link' # list of permissions

# Not working, "error" : "redirect_uri_mismatch"
config.omniauth :google_oauth2, ENV['OAUTH_GOOGLE_ID'], ENV['OAUTH_GOOGLE_SECRET']

omniauth_callbacks_controller.rb

def self.provides_callback_for(provider)
    class_eval %Q{
      def #{provider}
        @user = User.find_for_oauth(env["omniauth.auth"], current_user)

        if @user.persisted?
          handle_redirect("devise.#{provider}_uid", "#{provider}".capitalize)
        else
          session["devise.#{provider}_data"] = env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end
    }
  end

  [:facebook, :google_oauth2].each do |provider|
    provides_callback_for provider
  end

  def handle_redirect(_session_variable, kind)
    # here we force the locale to the session locale so it siwtches to the correct locale
    I18n.locale = session[:omniauth_login_locale] || I18n.default_locale
    sign_in_and_redirect @user, event: :authentication
    set_flash_message(:notice, :success, kind: kind) if is_navigational_format?
  end

omniauth_controller.rb

class OmniauthController < ApplicationController
  def localized
    # Just save the current locale in the session and redirect to the unscoped path as before
    session[:omniauth_login_locale] = I18n.locale
    redirect_to user_omniauth_authorize_path(params[:provider])
  end
end

routes.rb

  devise_for :users, skip: [:session, :password, :registration, :confirmation], controllers: { omniauth_callbacks: 'omniauth_callbacks' }


  localized do

    get 'auth/:provider' => 'omniauth#localized', as: :localized_omniauth

    devise_for :users, :path => 'accounts', skip: :omniauth_callbacks, :controllers => {sessions: 'sessions', registrations: 'registrations', passwords: 'passwords'}

end

用户.rb

devise :omniauthable, :omniauth_providers => [:facebook, :google_oauth2]

我在 localproduction 都有问题。

谁能帮帮我?我真的不知道我还能做什么。

【问题讨论】:

  • 您可以尝试使用http://127.0.0.1:3000/[PATH] 并使用该地址运行您的应用
  • 仍然无法正常工作... :-( 但这次我有这个:dropbox.com/s/3wok8pb7oob03ou/…
  • 第一次错误直接出现在我的控制台上
  • 好吧,我在this post 看到我不应该使用本地主机。所以我尝试使用 ngrok 但它并没有改变任何事情。 Picture 1Picture 2

标签: ruby-on-rails devise omniauth


【解决方案1】:

好了,终于知道是什么问题了……

'omniauth-google-oauth2'gem 仅适用于 'omniauth-oauth2' gem 的 1.3.1 版。

所以我像这样更改了我的 Gemfile:

# Social Networks Authentification
gem 'omniauth-oauth2', '~> 1.3.1' # Don't touch that unless you don't want Google omniauth to work!
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'omniauth-linkedin'
gem 'omniauth-google-oauth2' # Works only with omniauth-oauth2 v 1.3.1

现在一切正常!

更多详情请见this thread

【讨论】:

  • 好的,看来这在 2019 年仍然是一个问题,增加了 v1.3.1 使用已关闭的硬编码 Google Plus API 的复杂性。调查,
  • @Beejamin 您的调查结果如何?我目前面临同样的问题。
  • @MichaelHays,我正在使用'omniauth-oauth2', '~&gt; 1.6''omniauth-google-oauth2', '~&gt; 0.6.1',这是有效的。我不记得是否还有其他步骤 - 如果没有解决,请回复消息,我会进行挖掘!
  • @Beejamin 感谢您的提议!我最终采用了一种不同的方法,我试图将其添加到 omniauth-google-oauth2 作为文档:github.com/zquestz/omniauth-google-oauth2/issues/365
【解决方案2】:

请检查以下步骤以解决问题

步骤 1。错误:redirect_uri_mismatch 为以下错误,复制以下链接文本请求中的重定向 URI,http://yourlinks:3000/,与 OAuth 客户端授权的不匹配。访问...

步骤 2。登录到 Google Console Api,转到 Credential,选择您的 Api,然后通过上面的链接到 Authorized redirect URIs 部分

第 3 步。保存并再次测试

谢谢。

【讨论】:

    【解决方案3】:

    我有同样的问题,我犯了很多错误,我按照以下步骤纠正了:

    1. 首先检查google Credentials中的redirect url是否正确,
    2. 然后检查“omniauth-google-oauth2”的版本,更改此 gem 的版本对我有用。

    Gemfile 包含

    gem "gmail"
    gem "omniauth-google-oauth2"
    

    【讨论】:

      猜你喜欢
      • 2014-02-09
      • 2015-04-03
      • 2020-11-08
      • 2021-11-05
      • 1970-01-01
      • 2021-06-28
      • 2021-11-13
      • 1970-01-01
      • 2019-07-14
      相关资源
      最近更新 更多