通过rFacebook在Ruby上获得朋友的年龄

我正在创建一个Facebook应用程序,需要访问人们的年龄 – 只有用户的朋友,而不是普通大众。

我正在使用rFacebook版本0.6.2,沿着这条线设置。

我需要得到所有朋友的年龄/生日。

根据http://rfacebook.rubyforge.org/ rFacebook没有得到维护,它建议Facebooker,但即使Facebooker还没有在几个月内更新: https : //github.com/mmangino/facebooker

我建议考拉(而不仅仅是因为我是澳大利亚人)。 请阅读: https : //github.com/arsduo/koala还有关于在Rails上设置Koala的详细信息: https : //github.com/arsduo/koala/wiki/Koala-on-Rails

我上周刚刚使用考拉和自定义标签页面构建了一个FB应用程序,速度非常快。

您还需要阅读: http : //developers.facebook.com/docs/authentication/ (注意提及范围和权限级别)。 根据: http : //developers.facebook.com/docs/authentication/permissions/ ,当您请求范围时,您必须请求’friends_birthday’。

我不确定是否有一种简单的方法可以批量生活所有朋友的生日。 您可能需要遍历每个朋友并获取他们的信息。

作为测试,请访问: http : //developers.facebook.com/docs/reference/api/并单击friends链接。 然后复制第一个朋友的ID。 在URL中将’/ me / friends’替换为您复制的ID。 例如: https : //graph.facebook.com/me/friends ? access_token = ABC123变为https://graph.facebook.com/12345678?access_token=ABC123然后您将看到该朋友的数据,其中一个字段是生日。

#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 

虽然,您可以使用FQL进行批处理。

 #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) 

祝好运!

如上所述,FQL可能就是这里的方法。

几个笔记:

  1. 并非所有朋友都可以访问生日(因为他们限制了这些信息,或者因为他们没有发布信息)。 如果您只想为具有可访问生日的朋友提供数据,则可以在where子句中添加“和birthday_date”。
  2. 该查询不会返回所有数据,只会返回前100个左右。 如果你想获得所有这些,你需要一次请求一页。 您可以通过添加“limit 0,50”子句来执行此操作,从第0个开始请求50行。