你不需要omniauth-facebook。這個寶石的目的只是爲了獲取訪問令牌(並查詢用戶的信息)。 這裏的事情是如何發生的標準設置(與設計和omniauth Facebook的):
- Omniauth獲得它在請求的環境的訪問令牌
- Omniauth通過通過Facebook的API和商店獲取用戶的信息
- Omniauth調用應用程序中的一個自定義的回調方法,你需要寫
- 這個回調方法檢索或在數據庫中創建用戶,然後使用由設計提供了幾個助手登錄用戶。
(見Devise's documentation about Omniauth瞭解詳細信息)
。現在,因爲你已經有一個訪問令牌,以及從Facebook的API獲取用戶的信息很簡單,你實際上並不需要omniauth Facebook的。
所有你需要做的是寫一個動作:
- 獲得訪問令牌(作爲參數)
- 獲取用戶信息(一個Graph API請求/me)
- 發現或創建數據庫用戶(使用用戶的Facebook ID)
- 簽署用戶使用設計的助手
此代碼基於Devise's documentation中給出的示例。
class Users::FacebookCallbacksController
def facebook
# You should test if params[:access_token] is present
# and if this request fails
facebook_data = HTTParty.get("https://graph.facebook.com/me", query: {
access_token: params[:access_token]
}).parsed_response
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(facebook_data)
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = facebook_data
redirect_to new_user_registration_url
end
end
end
注: