2

我一直在尝试将 google auth 添加到我的注册页面并按照指南中的步骤进行操作,但是一旦我登录到我的 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”}

我已经尝试了这个现有的 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

路线.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://本地主机:3000/users/auth/google_oauth2/callback/

预先感谢您的回答!

4

1 回答 1

0

: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
}

希望有所帮助

于 2018-01-03T09:25:16.357 回答