7

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)

4 Answers 4

19

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.
6
  • Why? You've got to give me more.
    – runDOSrun
    Feb 18, 2015 at 18:36
  • I am sorry ! Actually, the second request "" requests.get(friends['paging']['next']).json() "" returns an object with friends['data'] as an empty list
    – Utsav T
    Feb 18, 2015 at 18:38
  • Is there an error? It works for me. On another note: You should check the Graph API explorer if it's returning the data you want. If not, it's probably not about the pagination but something else.
    – runDOSrun
    Feb 18, 2015 at 18:43
  • 1
    Not an error. Just that only the first set of friends are getting displayed ! I am just not able to figure out why
    – Utsav T
    Feb 18, 2015 at 18:49
  • It might be because of the "until" parameter in your URL. Once you've removed that, things should work fine.
    – ScreenSeer
    Aug 19, 2015 at 5:48
1

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.

3
  • getting an error facebook.GraphAPIError: Unknown path components: /friends&after=Q Dec 9, 2017 at 12:17
  • @kirankumarkotari Hi, can you please try after putting / on the path as I have edited it and see if it is working.
    – Shashank
    Dec 10, 2017 at 6:28
  • 1
    This doesnt work. friends = graph.get_connections("me", "/friends&summary=1") returns the same as graph.get_object('me')
    – M. Reyes
    Dec 18, 2017 at 19:06
1

I couldn't find this anywhere, these answers seem super complicated and just no way I would even use an SDK if I had do stuff like that when Paging from a simple POST is so easy to start with, however:

FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)

my_account = AdAccount('act_23423423423423423')


# In the below, I added the limit to the max rows, 250. 
# Also more importantly, paging. the SDK has a really sneaky way of doing this,
# enclose the request in a list() the results end up the same, but this will make the script request new objects until there are no more
#I tested this example and compared to Graph API and as of right now, 1/22 9:47AM, I get 81 from Graph and 81 here. 
fields = ['name']
params = {'limit':250}
ads = list(my_account.get_ads(
           fields = fields,
           params = params,
      ))

Trick from the docs: "NOTE: We wrap the return value of get_ad_accounts with list() because get_ad_accounts returns an EdgeIterator object (located in facebook_business.adobjects) and we want to get the full list right away instead of having the iterator lazily loading accounts."

https://github.com/facebook/facebook-python-business-sdk

0

in this example you off set / pagination by one at the time, i think my while loop is simple since it only looking for the pagination key"next" to be none, if doesnt exists means we finish looping, and you will have your results in a list. in this example i am just looking for all the people call jacob

import requests
import facebook

token = access_token="your token goes here"
fb = facebook.GraphAPI(access_token=token)
limit = 1
offset = 0
data = {"q": "jacob",
        "type": "user",
        "fields": "id",
        "limit": limit,
        "offset": offset}
req = fb.request('/search', args=data, method='GET')

users = []
for item in req['data']:
    users.append(item["id"])

pag = req['paging']
while pag.get("next") is not None:
    offset += limit
    data["offset"] = offset
    req = fb.request('/search', args=data, method='GET')
    for item in req['data']:
        users.append(item["id"])
    pag = req.get('paging')
print users

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.