Skip to content Skip to sidebar Skip to footer

Need To List All Friends With Facebook.py

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.Graph

Solution 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.

Solution 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')

Post a Comment for "Need To List All Friends With Facebook.py"