【问题标题】:Rails google_omniauth2 'error: redirect_uri_mismatch'Rails google_omniauth2 '错误:redirect_uri_mismatch'
【发布时间】:2018-01-03 00:07:44
【问题描述】:

我一直在尝试将 google auth 添加到我的注册页面并按照guide 中的步骤操作,但是一旦我登录到我的 google 帐户,我就会被重定向到 localhost:3000/# 并出现以下错误我的控制台:

E, [2018-01-02T18:21:05.351019 #57341] ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, redirect_uri_mismatch: 

{ “错误”:“redirect_uri_mismatch” }

我已经尝试了this existing stackoverflow 线程中的所有解决方案,但这些对这些用户有效的解决方案都没有对我有效。

我的设计.rb:

Devise.setup do |config|
config.stretches = Rails.env.test? ? 1 : 11
config.extend_remember_period = true
config.password_length = 2..72
config.sign_out_all_scopes = false
config.sign_out_via = :get



 OmniAuth.config.full_host = Rails.env.production? ? 
  'http://localhost:3000'
    config.omniauth(:facebook,
                  Rails.application.secrets.facebook_app_id,
                  Rails.application.secrets.facebook_api_key,
                  image_size: :large)
     config.omniauth(:google_oauth2,
                  Rails.application.secrets.google_client_id,
                  Rails.application.secrets.google_client_secret,
                  {:redirect_uri => 
     "http://localhost:3000/users/auth/google_oauth2/callback"}
     )
     require('devise/orm/active_record')
 end

routes.rb

  r.devise_for(:users,
             only:        :omniauth_callbacks,
             controllers: {omniauth_callbacks: :omniauth})

我还在我的谷歌开发者控制台中添加了以下重定向 uri:

https://localhost:3000/users/auth/google_oauth2/callback https://localhost:3000/users/auth/google_oauth2/callback/ http://localhost:3000/users/auth/google_oauth2/callback http://localhost:3000/users/auth/google_oauth2/callback/

提前感谢您的回答!

【问题讨论】:

  • 尝试从{:redirect_uri => "http://localhost:3000/users/auth/google_oauth2/callback"}删除主机(http://localhost:3000
  • 如果这不起作用,请将redirect_uri 替换为callback_path。这对我有用。
  • 我明白为什么这应该有效但仍然没有骰子。不过感谢您的尝试!

标签: ruby-on-rails devise omniauth google-developers-console omniauth-google-oauth2


【解决方案1】:

我的问题只有在添加了:name 选项时才会出现。删除后,它可以正常工作,如下所示:

config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], {
  :scope => "email, profile",
  :prompt => "select_account",
  :image_aspect_ratio => "square",
  :image_size => 50
}

希望能帮到你

【讨论】:

  • 我没有为范围、提示或 image_size 使用任何参数,但我添加它们只是为了看看它是否会改变任何东西。还是什么都没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 2018-03-21
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
相关资源
最近更新 更多