Devise / Google OAuth 2:找不到。身份验证passthru

时间:2017-03-24 02:46:28

标签: ruby-on-rails oauth devise oauth-2.0 google-oauth

我在自述omniauth-google-oauth2 gem的自述文件中遵循了教程,当我点击我的根(@ pages#home),<%= link_to "Sign up with Google", user_google_oauth2_omniauth_authorize_path %>上的链接时,我收到错误:

  

未找到。身份验证passthru。

我确认ENV vars在那里。我一直在寻找类似的主题而没有运气。知道我做错了什么吗?

在路线中:

Rails.application.routes.draw do
      devise_for :users, controllers: { :omniauth_callbacks => "users/omniauth_callbacks" }

我的omniauth_callbacks_controller位于/controllers/users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
      # You need to implement the method below in your model (e.g. app/models/user.rb)
      @user = User.from_omniauth(request.env["omniauth.auth"])

      if @user.persisted?
        flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
        sign_in_and_redirect @user, :event => :authentication
      else
        session["devise.google_data"] = request.env["omniauth.auth"].except(:extra) #Removing extra as it can overflow some session stores
        redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
      end
  end
end

在我的devise.rb文件中:

config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], {
      name: 'my-project',
      scope: 'email',
      prompt: 'select_account',
      image_aspect_ratio: 'original',
      image_size: 100,
      ssl_verify: false
  }

在我的User.rb中:

devise :rememberable, :validatable, :omniauthable, :omniauth_providers => [:google_oauth2]

   def self.from_omniauth(access_token)
         data = access_token.info
         user = User.where(:email => data["email"]).first

         # Uncomment the section below if you want users to be created if they don't exist
         # unless user
         #     user = User.create(name: data["name"],
         #        email: data["email"],
         #        password: Devise.friendly_token[0,20]
         #     )
         # end

         user
     end

4 个答案:

答案 0 :(得分:0)

值得检查您的Google OAuth重定向URI是否正确,最后包含/callback

答案 1 :(得分:0)

对于仍在寻找答案的任何人:

  1. 确保初始化文件夹中没有文件config/initializers/omniauth.rb
  2. config/initializers/devise.rb的最后一个config.omniauth参数处使用空白哈希,如下所示:
config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], {}

或者我们可以单独使用电子邮件范围。由于它将告诉Google我们通过电子邮件{ scope: "email" }

请求用户详细信息

答案 2 :(得分:0)

我解决了在 config/initializers/omniauth.rb 中添加以下内容的问题:

OmniAuth.config.allowed_request_methods = %i[get]

说明:

以上是https://github.com/zquestz/omniauth-google-oauth2#usage中的配置:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
end
OmniAuth.config.allowed_request_methods = %i[get]

但没有

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
end

因为这已经在您的 config/initializers/devise.rb 中提供:

  config.omniauth :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], {
      name: 'my-project',
      scope: 'email',
      prompt: 'select_account',
      image_aspect_ratio: 'original',
      image_size: 100,
      ssl_verify: false
  }

答案 3 :(得分:0)

我是这样解决这个问题的:

  1. 我将 gem omniauth-rails_csrf_protection 添加到 Gemfile 中
  2. 在我看来,我添加了 POST 方法
<%= link_to "Sign in with Google", 
    user_google_oauth2_omniauth_authorize_path,  method: :post %>
  1. 在我的devise.rb中:
Rails.application.config.middleware.use OmniAuth::Builder do
  OmniAuth.config.allowed_request_methods = [:post, :get]

  provider :google_oauth2, Rails.application.credentials[:GOOGLE_CLIENT_ID], 
      Rails.application.credentials[:GOOGLE_CLIENT_SECRET], {scope: "email"}
end
  1. 我的路线:
devise_for :users, controllers: {
  omniauth_callbacks: "users/omniauth_callbacks"
}

更多信息检查这个问题: [在此处输入链接描述][1]

https://github.com/heartcombo/devise/issues/5236