need to list all friends with facebook.py

旧时模样 提交于 2020-01-01 15:05:48

问题


i use facebook.py from: https://github.com/pythonforfacebook/facebook-sdk

my problem is: I don't know to use the next-url from graph.get_object("me/friends")

graph = facebook.GraphAPI(access_token)
friends = graph.get_object("me/friends")

回答1:


If you type in /me/friends into the Graph API Explorer, you'll see that it returns a JSON file, which is just a combination of dictionaries and lists inside one another.

For example, the output could be:

{
  "data": [
    {
      "name": "Foo", 
      "id": "1"
    }, 
    {
      "name": "Bar", 
      "id": "1"
    }
  ], 
  "paging": {
    "next": "some_link"
  }
}

This JSON file is already converted to a Python dictionary/list. In the outer dictionary, the key data maps to a list of dictionaries, which contain information about your friends.

So to print your friends list:

graph = facebook.GraphAPI(access_token)
friends = graph.get_object("me/friends")
for friend in friends['data']:
    print "{0} has id {1}".format(friend['name'].encode('utf-8'), friend['id'])

The .encode('utf-8') is to properly print out special characters.




回答2:


The above answer is mislead, as Facebook has shut down graph users from getting lists of friends UNLESS THE FRIENDS HAVE ALSO INSTALLED THE APP.

See:

graph   = facebook.GraphAPI( token )
friends = graph.get_object("me/friends")
if friends['data']:
  for friend in friends['data']:
    print ("{0} has id {1}".format(friend['name'].encode('utf-8'), friend['id']))
else:
  print('NO FRIENDS LIST')


来源:https://stackoverflow.com/questions/15234237/need-to-list-all-friends-with-facebook-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!