Ruby on rails GoogleOAuth2.0和Desive-“;错误";:&引用;重定向“uri不匹配”;

Ruby on rails GoogleOAuth2.0和Desive-“;错误";:&引用;重定向“uri不匹配”;,ruby-on-rails,devise,omniauth,Ruby On Rails,Devise,Omniauth,我将designe配置为将omniauth与Facebook和Google一起使用。它在Facebook上运行良好,但我在Google上遇到了一个问题 我一次又一次地遇到这个错误: 错误--omniauth:(google_oauth2)身份验证失败!无效的\u凭据:OAuth2::错误,重定向\u uri \u不匹配: {“错误”:“重定向\u uri\u不匹配”} [localhost][127.0.0.1][6a9377fe-d0b3-42] 运行时错误-找不到路径“/users/auth

我将
designe
配置为将omniauth与
Facebook
Google
一起使用。它在
Facebook
上运行良好,但我在
Google
上遇到了一个问题

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

错误--omniauth:(google_oauth2)身份验证失败!无效的\u凭据:OAuth2::错误,重定向\u uri \u不匹配:

{“错误”:“重定向\u uri\u不匹配”}

[localhost][127.0.0.1][6a9377fe-d0b3-42] 运行时错误-找不到路径“/users/auth/google\u oauth2/callback”的有效映射:

designe(3.5.2)lib/designe/mapping.rb:49:in'find_by_path!'

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

Gemfile

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

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\u回调\u控制器.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
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
omniauth_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
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
user.rb

devise :omniauthable, :omniauth_providers => [:facebook, :google_oauth2]
我在本地和生产中都有这个问题


有人能帮我吗?我真的不知道我还能做些什么。

好吧,终于找到了问题所在

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

所以我改变了我的文件如下:

# 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
现在一切都很好


有关更多详细信息,请参阅。

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

第一步。错误:重定向uri不匹配由于以下错误,请复制以下链接文本请求中的重定向uri与OAuth客户端授权的uri不匹配。访问

第二步。登录谷歌控制台Api,转到凭证,选择您的Api并通过上面的链接进入授权重定向URI部分

第三步。保存并再次测试


谢谢。

我也有同样的问题,犯了很多错误,我通过以下步骤纠正了这些错误:

  • 首先检查google凭据中的重定向url是否正确
  • 然后检查“omniauth-google-oauth2”的版本,更改这个gem的版本对我来说很有用
  • Gemfile包含

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

    你能试试使用
    http://127.0.0.1:3000/[路径]
    并在该地址仍不工作的情况下运行应用程序…:-(但这次我有这样一个问题:第一次直接在我的控制台上出错时,我发现我不应该使用localhost。所以我尝试使用ngrok,但它没有改变任何事情。好吧,看来这在2019年仍然是一个问题,更复杂的是v1.3.1使用了已经关闭的硬编码Google Plus API。Investigating,@Beejamin你的调查结果是什么?我目前面临着同样的问题。@MichaelHays,我使用的是
    'omniauth-oauth2'、“~>1.6'
    'omniauth-google-oauth2'、“~>0.6.1'
    ,这是有效的。我不记得是否还有其他步骤-如果没有解决,请回复消息,我会继续挖掘!@Beejamin感谢您的提议!我最终选择了另一种方法,我正试图将其作为文档添加到
    omniauth-google-oauth2