我正在尝试配置一个新的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 看起来不错,那么为什么会像失败一样处理呢?
配置/初始化程序/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'
路线.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
应用程序控制器.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
My gems:
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