开发者

Get age of friend on Facebook with Ruby via rFacebook

I am creating a Facebook app and need to access people's ages - only the friends of the user, not the general public.

I am using rFacebook version 0.6.2, setup along the line of thi开发者_StackOverflow社区s.

I need to get the ages/ birthdays of all of my friends.


As per http://rfacebook.rubyforge.org/ rFacebook isnt being maintained, it suggested Facebooker, but even Facebooker hasn't been updated in months: https://github.com/mmangino/facebooker

I suggest Koala (and not just because I'm an Aussie). Have a read at: https://github.com/arsduo/koala There's also details on setting Koala up on Rails: https://github.com/arsduo/koala/wiki/Koala-on-Rails

I just built a FB app using Koala and a Custom Tab Page last week and it was very quick.

You also need to read: http://developers.facebook.com/docs/authentication/ (pay attention to the mention of scopes and permission levels). As per: http://developers.facebook.com/docs/authentication/permissions/ you must request 'friends_birthday' when you request your scope.

I'm not sure there is an easy way to get all your friends' birthdays in a batch. You might have to traverse each friend and get their info.

As a test, go to: http://developers.facebook.com/docs/reference/api/ and click on the friends link. Then copy the ID of your first friend. In the URL replace '/me/friends' with the ID you copied. Eg: https://graph.facebook.com/me/friends?access_token=ABC123 becomes https://graph.facebook.com/12345678?access_token=ABC123 You will then see the data of that friend, one field of which is birthday.

#i have already asked for user permissions, and have my access token
#https://github.com/arsduo/koala/wiki/OAuth
graph = Koala::Facebook::GraphAPI.new(oauth_access_token)
friends = graph.get_connections("me", "friends")
friends.each do |f|
   friend = graph.get_object(f['id'])
   puts "#{f['name']} has a birthday on #{friend["birthday"]}"
end

Although, you might be able to use FQL to do a batch.

#FQL taken from http://stackoverflow.com/questions/5063431/easiest-way-to-get-birthday-info-of-all-friends-thorugh-graph-api 
fql = "select uid,name,birthday_date from user where uid in (select uid2 from friend where uid1=me())"
#https://github.com/arsduo/koala/wiki/REST-API
@rest = Koala::Facebook::GraphAndRestAPI.new(oauth_access_token)
birthdays = @rest.fql_query(fql)

Good luck!


FQL is probably the way to go here as is mentioned above.

A couple notes:

  1. Not all of your friends will have a birthday accessible (either because they restricted that information, or because they didn't post it). If you only want data for your friends with accessible birthdays, you can add a "and birthday_date" to your where clause.
  2. That query will not return all data, but only the first 100 or so. If you want to get all of them, you will need to request them one page at a time. You can do this by adding a "limit 0,50" clause, to request 50 rows, starting at the 0th one.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜