ruby-on-rails - Facebook、Linkedin 和 Google 的 Oauth2 登录停止与 Devise 和 Omniauth 合作,但仍适用于 LinkedIn 和 Twitter

标签 ruby-on-rails devise google-oauth omniauth-facebook omniauth-linkedin

我有一个站点配置为使用 Devise 和 Omniauth 与多个 Oauth2 API 一起工作,直到上周才正常运行。目前使用 Twitter 和 Github 登录仍然可以正常使用;然而,Facebook、LinkedIn 和 Google 给我一个错误,指出重定向 URI 不匹配。错误信息如下:

Facebook:

ERROR -- omniauth: (facebook) Authentication failure! invalid_credentials: >OAuth2::Error, : {"error":{"message":"Error validating verification code. Please make sure your >redirect_uri is identical to the one you used in the OAuth dialog request","type":"OAuthException","code":100,"fbtrace_id":"XXXXXXXXXX"}}

LinkedIn:

ERROR -- omniauth: (linkedin) Authentication failure! invalid_credentials: >OAuth2::Error, invalid_request: missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired {"error_description":"missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired","error":"invalid_request"}

Google

ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: >OAuth2::Error, redirect_uri_mismatch: { "error" : "redirect_uri_mismatch" }

我查看了在 Chrome 开发者控制台中为所有这三个发送的请求,回调的重定向 uri 与每个 API 注册的 uri 相匹配(自运行以来没有改变)。

回溯这个错误的挑战是我不能 100% 确定这些停止工作是因为我直接登录还是在最近的集成测试中安装新功能时使用 Github 登录。 (重要的教训!)可能会影响这一点的重大变化之一是我集成了 Devise 的 Traceable 扩展,这让我需要 Warden Gem。但是,我删除了 Traceable 和 Warden 配置,并将用户模型和配置文件恢复到之前的状态,但我遇到了同样的问题。

我通常更愿意提供更多代码示例,但老实说,我不确定从什么代码开始。我希望有人遇到过类似的问题,并能指出正确的开始方向。

首先,下面是我的 Devise Initializer,删除了注释以缩短

Devise.setup do |config|

  config.mailer_sender = 'no-reply@' + ENV['DOMAIN_NAME']

  config.mailer = 'Devise::Mailer'

  require 'devise/orm/active_record'

  config.case_insensitive_keys = [:email]

  config.strip_whitespace_keys = [:email]

  config.skip_session_storage = [:http_auth]

  config.stretches = Rails.env.test? ? 1 : 10

  config.allow_unconfirmed_access_for = 10.days

  config.reconfirmable = true

  config.confirmation_keys = [:email]

  config.remember_for = 2.weeks

  config.expire_all_remember_me_on_sign_out = true

  config.password_length = 8..72

  config.email_regexp = /\A[^@]+@[^@]+\z/

  config.reset_password_keys = [:email]

  config.reset_password_within = 6.hours

  config.sign_in_after_reset_password = true

  config.sign_out_via = :get

  # ==> OmniAuth
  # Add a new OmniAuth provider. Check the wiki for more information on setting
  # up on your models and hooks.
  # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'

  require "omniauth-google-oauth2" # Added Based on Response to Another Stackoverflow Issues - Did Not Help.

  OMNIAUTH = YAML.load(File.read(File.expand_path('../../omniauth.yml', __FILE__))).deep_symbolize_keys

  OMNIAUTH.each_value do |provider|
    config.omniauth provider[:reference].to_sym, ENV[provider[:key_ref]], ENV[provider[:secret_ref]], { :scope => provider[:scope] }
  end
end

加载的 omniauth.yml 文件如下所示:

facebook: { reference: "facebook",
            name: "Facebook",
            scope: "email, public_profile, user_birthday",
            key_ref: "FACEBOOK_KEY",
            secret_ref: "FACEBOOK_SECRET" }

twitter:  { reference: "twitter",
            name: "Twitter",
            scope: "r_fullprofile, r_emailaddress",
            key_ref: "TWITTER_KEY",
            secret_ref: "TWITTER_SECRET" }

linkedin: { reference: "linkedin",
            name: "LinkedIn",
            scope: "r_basicprofile r_emailaddress",
            key_ref: "LINKEDIN_KEY",
            secret_ref: "LINKEDIN_SECRET" }

github: {   reference: "github",
            name: "GitHub",
            scope: "user, public_repo",
            key_ref: "GITHUB_KEY",
            secret_ref: "GITHUB_SECRET" }

google:   { reference: "google_oauth2",
            name: "Google",
            scope: "email, profile",
            key_ref: "GOOGLE_KEY",
            secret_ref: "GOOGLE_SECRET" }

最佳答案

我有完全相似的问题,facebook 工作,linkedin 和 google - 不。

经过一些挖掘/谷歌搜索后,我能够通过降级来解决我的问题:

gem 'omniauth-oauth2', '1.3.1'

所以我的 Gemfile 看起来像:

gem 'devise'
gem 'koala'
gem 'omniauth-oauth2', '1.3.1'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
gem 'omniauth-linkedin-oauth2'

关于ruby-on-rails - Facebook、Linkedin 和 Google 的 Oauth2 登录停止与 Devise 和 Omniauth 合作,但仍适用于 LinkedIn 和 Twitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33352426/

相关文章:

ruby-on-rails - 使用 Devise Gem 自定义密码保存

oauth - 每次我提出请求时,Youtube oAuth 提示授权窗口

javascript - 无需 OAuth2 重定向/复制和粘贴即可访问私有(private) Google 云端硬盘数据

ruby-on-rails - 当部署到 Heroku 时,我得到 ActionView::Template::Error (缺少部分

ruby-on-rails - 运行单个迁移文件

ruby-on-rails - 运行从 GitHub 下载的 Rails 应用程序 (database.yml)

ruby - Rails 渲染 Controller 中的部分和布局

ruby-on-rails - 由于 ruby​​gems (LoadError),无法在 Rails 应用程序中运行测试

ruby-on-rails-3 - 跨子域的 rails 3 session 在 Internet Explorer 中不起作用

java - 谷歌 OAuth : how to use a refresh token?