我發現使用OAuth並在fb_graph寶石(如指定here和here)設計的最好方法。我曾與版本的問題,所以我的Gemfile配置是這樣的:
gem 'devise', :git => 'git://github.com/plataformatec/devise.git'
gem 'omniauth', '>=0.2.0.beta'
gem 'oa-oauth', :require => 'omniauth/oauth'
gem 'fb_graph'
(我的配置是很老 - 這可能是設計出最新的分支,現在更亂裝即用的方式支持omniauth) 。
我色器件初始化:
config.omniauth :facebook_publish, "APP_ID", "APP_SECRET"
在用戶模式:
devise :omniauthable
和
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token['extra']['user_hash']
if user = User.find_by_email(data["email"])
user
else # Create a user with a stub password.
User.create(:email => data["email"], :password => Devise.friendly_token[0,20])
end
end
爲了Facebook的連接 - 去路徑:
user_omniauth_authorize_path(:facebook)
基本上這就是連接所需的全部。爲了得到圖我現在使用:
facebook_user = FbGraph::User.new('me', :access_token => access_token['credentials']['token']).fetch
而對於朋友:
facebook_user.friends
就是這樣。希望能幫助到你。
我知道這是相當古老的,但你在哪裏把facebook_user = ... FBGraph部分? – stewart715
在控制器或模型中(您需要獲取好友列表)。 – Drakmail