开发者

Retrieve friends locations from Facebook using fb_graph Ruby gem

I'm trying to retrieve开发者_运维技巧 the locations of all of a user's friends using the gem: fb_graph. (version 1.7.2)

My permissions are: publish_stream,read_friendlists,offline_access,friends_location,user_location

I've already authenticated the user and stored the offline access token and their facebook id.

user = User.find_by_email("person@email.com")
facebook_user = FbGraph::User.fetch(user.facebook_identifier, :access_token => user.facebook_access_token)
facebook_user.friends.map(&:location) => [nil, nil...]

When I check the permissions from within Facebook it says I have access to friend locations:

http://grab.by/a1Qm

My problem is when I try and get the locations of my friends it always returns nil. I have permissions setup correctly and I know the location should not be nil for everybody.

Does anybody have any suggestions or pointers on how to go about getting friend locations?

Thanks!


So I as able to get the information I needed. You have to call fetch after each call, so

user = User.find_by_email("email@domain.com")

facebook_user = FbGraph::User.fetch(user.facebook_identifier, :access_token => user.facebook_access_token)
friends = facebook_user.fetch.friends
friends.each do |friend|
  location = friend.fetch.location
  puts location.try(:name)
end

This gets me the data that I want, however it is rather slow iterating over everything and doing an individual call for each piece of data so I refactored to just use straight FQL to return all the data that I needed.

friend_data = FbGraph::Query.new("SELECT uid, first_name, last_name, pic_square, current_location FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1='#{user.facebook_identifier}')").fetch(user.facebook_access_token)


API permissions do not override a user's privacy settings. Is it possible that your facebook user privacy settings block this? Or perhaps all of your friends' privacy settings block this?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜