【问题标题】:Google oauth2 with devise and omniauth processed as failure带有设计和omniauth的Google oauth2被处理为失败
【发布时间】:2016-04-17 02:21:19
【问题描述】:

我正在尝试配置一个新的 rails4.2 应用来针对 Google Oauth2 进行身份验证。

我似乎成功地完成了这个过程,但它被视为失败。

在谷歌发送到回调之前,初始授权似乎进展顺利。那么它似乎被错误地识别为失败。

给出的错误信息是: Could not authenticate you from Google because "Invalid credentials".

我已经搜索过解决方案,但无济于事。

是否可以打开额外的日志记录以了解它为什么选择通过失败方法进行处理?

这是一个请求的日志:

Started GET "/users/auth/google" for 127.0.0.1 at 2016-04-17 09:37:33 +0800
Started GET "/users/auth/google/callback?state=<<state>>&code=<<code>>" for 127.0.0.1 at 2016-04-17 09:37:45 +0800
Processing by Users::OmniauthCallbacksController#failure as HTML
  Parameters: {"state"=>"<<state>>", "code"=>"<<code>>"}
Redirected to http://test_app.dev/sign_in
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)

测试的时候,google提示我的时候点击了允许,而且url看起来不错,为什么会被当作失败处理呢?

config/initializer/devise.rb

  config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ['GOOGLE_CLIENT_SECRET'],
         :strategy_class => OmniAuth::Strategies::GoogleOauth2,
         :name => 'google',
         :scope => 'email,profile,contacts',
         :access_type => 'offline',
         :image_aspect_ratio => 'square'

routes.rb

  devise_for :users, :controllers => { omniauth_callbacks: 'users/omniauth_callbacks' }
  resources :users

  devise_scope :user do
    get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
    get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
  end

控制器/用户/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google
      logger.debug 'Omniauth callback called' # Never get's called
  end
end

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  # Direct to user profile after sign in
  def after_sign_in_path_for(resource)
    user_path(current_user)
  end

  # Needed by Devise when using omniauth
  def new_session_path(scope)
    new_user_session_path
  end
end

我的宝石:

Using warden 1.2.6
Using devise 3.5.6
Using oauth2 1.0.0
Using omniauth 1.2.2
Using omniauth-oauth2 1.4.0
Using omniauth-google-oauth2 0.4.1

【问题讨论】:

    标签: ruby-on-rails devise omniauth ruby-on-rails-4.2


    【解决方案1】:

    简短的回答是因为你的信用是错误的。您在配置哈希中的第一个参数而不是第二个参数上调用 ENV。

    更好的答案是……使用更好的捕鼠器。

    有时使用 ENV 存储密钥可能会出现问题,您可能没有在启动服务器的同一终端中加载密钥,或者如果您在生产中,您可能无法使用查看 ENV 来了解它缺少钥匙。使用秘密文件更容易。没关系,rails 正是出于这个原因才提供它。

    config/secrets.yml
    

    您可以在其中以 yml 格式存储您想要的任何密钥。确保将文件添加到您的 .gitignore 中,因为您绝对不想将带有密钥的文件存储在某处的存储库中。您必须手动将机密文件复制到生产服务器。

    development:
      omniauth_provider_key: 13232423423242315
      omniauth_provider_secret: 2222222222228eff721a0322c
      domain_name: lvh.me
      secret_key_base: 6ec9ae65d4de59aa1a7ssxxsdifwn9392203905c53a264ffd8255a601d7417b1ed7d4cef67f359e373472f0160aeb9698fa69578a1497b5b99209afd0e
    

    production stagingtest 也可以具有相同的结构

    现在.. 完成此操作(创建文件并将密钥添加到其中)现在您可以从初始化程序调用密钥

      config.omniauth :google_oauth2, Rails.application.secrets.omniauth_provider_key, Rails.application.secrets.omniauth_provider_secret,
         :strategy_class => OmniAuth::Strategies::GoogleOauth2,
         :name => 'google',
         :scope => 'email,profile,contacts',
         :access_type => 'offline',
         :image_aspect_ratio => 'square'
    

    【讨论】:

    • 拍前额谢谢。感谢您提供有关 secrets.yml 的信息,但我不知道这一点,但是,我正在使用 heroku 进行生产,并且在 heroku 上快速搜索解决方案似乎表明将 ENV[...] 放入秘密中的一些变化.yml
    • 我们都有自己的日子 :) 如果你有兴趣在 herkou 中使用秘密,那么考虑 figaro - 有一个 rake 任务,rake figaro: heroku - 它可以让你进行原本会做的配置在带有 env 的系统上。 --- github.com/laserlemon/figaro
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多