Add Facebook Friends with Rails 3 + OmniAuth + FBGraph

I'm currently trying to write the step of adding friends to our registration system, and I was asked to implement something similar to how foursquare works.

He pulls your friends from Facebook, checks to see if any of them are already on the foursquare website, and then asks if you want to contact them.

What is the best way to implement this kind of function?

I have seen examples with the FB JS SDK, however I would prefer a more integrated approach.

Thanks Adam

+2
ruby-on-rails facebook ruby-on-rails-3 facebook-graph-api
source share
1 answer

The best way I've found is to use devess with oauth and fb_graph gems (as stated here and here ). I had a versioning issue, 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 is possible that the development of the last branch now supports omniauth in a more finished form). A.

my 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 

To connect to facebook, go along the path:

 user_omniauth_authorize_path(:facebook) 

Basically, this is all you need to connect. To get the graph, I now use:

  facebook_user = FbGraph::User.new('me', :access_token => access_token['credentials']['token']).fetch 

And for friends:

 facebook_user.friends 

What is it. Hope this helps.

+3
source share

All Articles