Rails, Facebook API, Koala gem — get all profiles

2020-06-24 06:04发布

Using Rails3 and koala gem, how to retrive all profiles of users who 'Like' a FB page (http://facebook.com/DAKINE for example). Is it possible at all? As the final result i need to get a bunch of user profiles, stored in db.

Thanks!

1条回答
做个烂人
2楼-- · 2020-06-24 06:56

It's pretty easy to get the likes of any page on facebook, particularly with the Koala gem. Since the facebook likes for a given page are publically accessible (see here), you can access them without using an API key.

For example, to obtain the number of likes for this page: facebook.com/foofighters you could do:

graph = Koala::Facebook::GraphAPI.new
likes = graph.get_object("foofighters")["likes"]

I'm assuming you have your user profiles stored in your application database, and each (or some) of these have associated facebook IDs? If you're trying to save the 'like' data locally for your users, you could do something along the lines of:

graph = Koala::Facebook::GraphAPI.new
@users.each do |user|
  user.likes = graph.get_object(user.facebook_id)["likes"]
  user.save
end
查看更多
登录 后发表回答