从Facebook加入Rails 3 + OmniAuth + FBGraph的朋友 [英] Add friends from facebook with Rails 3 + OmniAuth + FBGraph
问题描述
我正在尝试为我们的注册系统添加一个朋友的步骤,我被要求实现类似于foursquare的工作。
它从Facebook下拉你的朋友,检查他们是否已经在四方网站上,然后询问你是否要与他们联系。
什么是最好的方法来实现这种功能?
我看过FB JS SDK的例子,但是我更喜欢采用更加集成的方法。
感谢
Adam
我发现最好的方法是使用devise使用oauth和fb_graph gems(如此处和 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
/ pre>
和
def self.find_for_facebook_oauth(access_token,signed_in_resource = nil)
data = access_token ['extra'] ['user_hash']
如果user = User.find_by_email(data [email])
user
else#创建具有存根密码的用户。
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
就是这样。希望有帮助。
I'm currently trying to write an add friends step to our registration system, and I've been asked to implement something similar to the way foursquare works.
It pulls down your friends from Facebook, checks to see if any of them are already on the foursquare site, and then asks if you want to connect with them.
What is the best way to go about implementing this kind of functionality?
I have seen examples with the FB JS SDK, however I would prefer a more integrated approach.
Thanks Adam
解决方案The best way I found is using devise with oauth and the fb_graph gems (like specified here and here). I had an issue with the versions so my gemfile configuration looks like this:
gem 'devise', :git => 'git://github.com/plataformatec/devise.git' gem 'omniauth', '>=0.2.0.beta' gem 'oa-oauth', :require => 'omniauth/oauth' gem 'fb_graph'
(my configuration is quite old - it's possible that devise latest branch now supports omniauth in a more out-of-the-box way).
my devise initializer:
config.omniauth :facebook_publish, "APP_ID", "APP_SECRET"
In your User model:
devise :omniauthable
and
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
In order to facebook connect - go to path:
user_omniauth_authorize_path(:facebook)
Basically that is all you need for the connection. In order to get the graph I now use:
facebook_user = FbGraph::User.new('me', :access_token => access_token['credentials']['token']).fetch
And for the friends:
facebook_user.friends
And that's it. Hope it helps.
这篇关于从Facebook加入Rails 3 + OmniAuth + FBGraph的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!