![](/img/trans.png)
[英]Is there a working Devise/Omniauth/Oauth2 interface with Wordpress?
[英]Google oauth2 with devise and omniauth processed as failure
我正在嘗試配置一個新的rails4.2應用程序以針對Google Oauth2進行身份驗證。
我似乎已經成功完成了該過程,但是將其視為失敗。
在google發送給回調之前,初始授權似乎進行得很順利。 然后,它似乎被錯誤地識別為故障。
給出的錯誤消息是: 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=<<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提示我時,我單擊了允許,並且該網址看起來不錯,那么為什么要像處理失敗一樣進行處理呢?
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
簡短的答案是,因為您的信譽是錯誤的。 您正在配置哈希中的第一個而不是第二個參數上調用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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.