将设计和omniauth处理为失败的Google oauth2
我正在尝试配置一个新的rails4.2应用来对Google Oauth2进行身份validation。
我似乎成功地完成了整个过程,但它被视为失败。
最初的授权似乎进展顺利,直到谷歌发送回调。 然后它似乎被错误地识别为失败。
给出的错误消息是: Could not authenticate you from Google because "Invalid credentials".
我已经google了解决方案,但无济于事。
是否可以打开其他日志记录以了解它为什么选择通过故障方法进行处理?
这是请求的日志:
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=<>&code=<>" for 127.0.0.1 at 2016-04-17 09:37:45 +0800 Processing by Users::OmniauthCallbacksController#failure as HTML Parameters: {"state"=>"<>", "code"=>"<>"} Redirected to http://test_app.dev/sign_in Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
在测试时,我点击了谷歌提示我的时候允许,并且url看起来很好,那么为什么这个被处理好像是失败的呢?
配置/初始化/ 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
我的gem:
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
简短的回答是因为你的信用是错误的。 你在第一个调用ENV而不是在你的配置哈希中的第二个参数上调用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
staging
或test
现在..一旦你完成了(创建文件并将密钥添加到它),现在你可以从初始化程序调用密钥
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'
- Devise中的ArgumentError :: RegistrationsController#新错误的参数个数(2个为0..1)
- 当每个请求的app id和app secret不同时,请使用omniauth-facebook?
- 在rails应用程序中使用OmniAuth时,为什么我总是遇到Errno :: ETIMEDOUT
- 单一登录与设计和OmniAuth
- 从facebook中获取用户个人资料大图片,并在设计2.0中使用omniauth
- Mendeley定制OAuth战略
- OmniAuth before_filter要求登录
- OmniAuth Google OpenID WEBrick :: HTTPStatus :: RequestURITooLarge
- 为什么我在前2位用户之后收到“此授权码已被使用”?