Python Facebook API - cursor pagination

My question involves learning how to retrieve my entire list of friends using Facebook's Python API. The current result returns an object with limited number of friends and a link to the 'next' page. How do I use this to fetch the next set of friends ? (Please post the link to possible duplicates) Any help would be much appreciated. In general, I need to learn about the pagination involved the API usage.

import facebook
import json

ACCESS_TOKEN = "my_token"

g = facebook.GraphAPI(ACCESS_TOKEN)

print json.dumps(g.get_connections("me","friends"),indent=1)

Solution 1:

Sadly the documentation of pagination is an open issue since almost 2 years. You should be able to paginate like this (based on this example) using requests:

import facebook
import requests

ACCESS_TOKEN = "my_token"
graph = facebook.GraphAPI(ACCESS_TOKEN)
friends = graph.get_connections("me","friends")

allfriends = []

# Wrap this block in a while loop so we can keep paginating requests until
# finished.
while(True):
    try:
        for friend in friends['data']:
            allfriends.append(friend['name'].encode('utf-8'))
        # Attempt to make a request to the next page of data, if it exists.
        friends=requests.get(friends['paging']['next']).json()
    except KeyError:
        # When there are no more pages (['paging']['next']), break from the
        # loop and end the script.
        break
print allfriends

Update: There's a new generator method available which implements above behavior and can be used to iterate over all friends like this:

for friend in graph.get_all_connections("me", "friends"):
    # Do something with this friend.

Solution 2:

Meanwhile I was searching answer here is much better approach:

import facebook
access_token = ""
graph = facebook.GraphAPI(access_token = access_token)

totalFriends = []
friends = graph.get_connections("me", "/friends&summary=1")

while 'paging' in friends:
    for i in friends['data']:
        totalFriends.append(i['id'])
    friends = graph.get_connections("me", "/friends&summary=1&after=" + friends['paging']['cursors']['after'])

At end point you will get one response where data will be empty and then there will be no 'paging' key so at that time it will break and all the data will be stored.