Ruby on rails 如何通过编程获得FB喜欢的数量
在Rails应用程序中使用考拉,我需要获得动态页面的Facebook喜好总数 例如,如果有一个用户模型,我想获得每个用户的显示页面的喜欢数,并将其存储在User.likes属性中 我面临两个挑战:Ruby on rails 如何通过编程获得FB喜欢的数量,ruby-on-rails,koala,Ruby On Rails,Koala,在Rails应用程序中使用考拉,我需要获得动态页面的Facebook喜好总数 例如,如果有一个用户模型,我想获得每个用户的显示页面的喜欢数,并将其存储在User.likes属性中 我面临两个挑战: 考拉get_object方法需要Facebook UID。如何从页面的url获取页面的UID 使用Facebook open graph explorer,哈希似乎不包括喜欢的数量?如何从开放图中访问喜欢的数量 我最终设法解决了这个问题的两个部分。最后,我完全放弃了考拉的get_对象,直接进入打开的图
get_object
方法需要Facebook UID。如何从页面的url获取页面的UID我最终设法解决了这个问题的两个部分。最后,我完全放弃了考拉的get_对象,直接进入打开的图形 首先,我需要在我的应用程序中找到表示感兴趣页面的open graph对象。我在
http://graph.facebook.com.?ids=http://my.app.com/user/xx
这返回了一个json对象,我需要对其进行解析
最后,我需要从json对象访问正确的键/值
所以把这些都放在我的用户模型上
class User
def facebook_likes
user_url = UrlGenerator.new.user_url(self)
#UrlGenerator is a custom module that allows
#access to Rails route helpers in the model,
#no doubt some will consider this bad practice but
#in this case I believe it is justified
graph_url = "https://graph.facebook.com/?ids=#{user_url}"
graph_object = JSON.parse( open( graph_url ).read )
likes = graph_object["#{UrlGenerator.new.candidate_url(self)}"]["shares"] || 0 #returns 0 if no likes found
end
end
如果有更好的方法,我愿意接受建议我的答案是否正确?