【问题标题】:Devise/Google OAuth 2: Not found. Authentication passthru设计/Google OAuth 2:未找到。身份验证通路
【发布时间】:2017-08-16 20:43:18
【问题描述】:

我按照omniauth-google-oauth2 gem 的自述文件中的教程进行操作,当我单击我的根目录 (@pages#home)、<%= link_to "Sign up with Google", user_google_oauth2_omniauth_authorize_path %> 上的链接时,出现错误:

未找到。身份验证通道。

我已经确认 ENV 变量在那里。我一直在寻找类似的主题,但没有运气。知道我做错了什么吗?

在路线中:

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

【问题讨论】:

  • 您是否考虑过在 GitHub 项目上发布问题?作者或许能帮到你。
  • 我没有。这不是 gem 的问题,似乎我做错了一些小事。
  • 你确定你的路线user_google_oauth2_omniauth_authorize_path 好吗? rake routes | grep omni 得到了什么?

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


【解决方案1】:

我解决了问题,将以下内容添加到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
  }

【讨论】:

    【解决方案2】:

    值得检查您的 Google OAuth 重定向 URI 是否正确,并在末尾包含 /callback

    【讨论】:

    • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
    • @Iceman - 感谢您的反馈,但请不要将答案的提问方式请求澄清混淆。 OP 已经清楚地遵循了 gem 的说明,并表示已经检查了 env vars。重定向 URI 是文档中未包含的一件事,它可能是导致此错误的原因。所以(至少在我看来)它一个潜在的答案......我已经对其进行了编辑以更清楚地表明这实际上不是一个问题:-)
    【解决方案3】:

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

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

    或者我们可以单独使用电子邮件范围。因为它会告诉谷歌我们通过电子邮件{ scope: "email" } 要求用户详细信息

    【讨论】:

      【解决方案4】:

      我这样解决了这个问题:

      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

      【讨论】:

      • 欢迎来到 SO。在回答已经有答案的旧问题时,请尝试解释为什么您的问题与之前的答案不同或有所改进。
      猜你喜欢
      • 2017-05-10
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      相关资源
      最近更新 更多