Get your hand dirty — Facebook Graph API for the first time, PART 2

Kiu Lam
3 min readJun 25, 2017

Previously, in Part 1, we learned how to use the Graph API Explorer to explore the Facebook Graph API. Now we can begin writing a simple computer program to retrieve the desire data. The goal of this program is to collect the most recent 9002 feeds from Hackathon Hacker

I am going to use Python3 with “FacePy” library for the sake of simplicity.

After installing Facepy, go ahead to prepare the GraphAPI wrapper following

from facepy import GraphAPI
from pprint import pprint as pp
# Initialize the Graph API with a valid access token (optional,
# but will allow you to do all sorts of fun stuff).
graph = GraphAPI(user_access_token) # Paste user_access_token here

After locating the group’s id, prepare the GET request for the group

from facepy import GraphAPI
from pprint import pprint as pp
# Initialize the Graph API with a valid access token (optional,
# but will allow you to do all sorts of fun stuff).
graph = GraphAPI(user_access_token) # Paste user_access_token here
# Locate the group id successfully
hackathon_hacker_group_id = '759985267390294'
# Get the latest feeds
feeds = graph.get(hackathon_hacker_group_id + '/feed')

In this case, I would only wanna know who posted the feed, and what are the messages. Go ahead to give the GET request the fields

from facepy import GraphAPI
from pprint import pprint as pp
# Initialize the Graph API with a valid access token (optional,
# but will allow you to do all sorts of fun stuff).
graph = GraphAPI(user_access_token) # Paste user_access_token here
# Locate the group id successfully
hackathon_hacker_group_id = '759985267390294'
# Get the latest feeds
feeds = graph.get(path=hackathon_hacker_group_id + '/feed', fields='from,message')

If you run the program now, you should be able to see the similar result following

{'data': [{'from': {'id': '312726882421620', 'name': 'Sandeep Acharya'},
'id': '759985267390294_1651146688274143',
'message': 'Got the Google FooBar invite. Any suggestions?',
'updated_time': '2017-06-25T01:30:35+0000'},
{'from': {'id': '824800630898785', 'name': 'Edwin Carbajal'},
'id': '759985267390294_1651558858232926',
'message': 'Hey gang! Just published my first chrome extension! '
'Manage all your personal links in one place for job '
"applications 🤓 Let me know what you think and don't "
'forget to rate & review <3\n'
'\n',
'updated_time': '2017-06-25T01:21:57+0000'},
{'from': {'id': '792701810777764',
'name': 'Armando Alexis Herra Cortez'},
'id': '759985267390294_1651531058235706',
'message': 'Any thoughts, tips, tricks on Unit Testing and e2e '
"Testing? I'm checking out Jasmine and Karma.",
...
},
'paging': {'next': 'https://graph.facebook.com/v2.3/759985267390294/feed?fields=from,mess...8wDfjN92EQDZBlAZDZD',
'previous': 'https://graph.facebook.com/v2.3/759985267390294/feed?fields=from,message&icon_size=16&since=1498354235...NkrpjBISiJUS8gZDZD&__previous=1'}
}

Python would return the GET request result in a dictionary. The value of the “data” key is a list of dictionaries, each dictionary contains the information of the feed.

Since I want to collect the most recent 10000 feeds, thus increase the limit higher, go ahead to assign a limit with higher value

# Get the latest feeds with higher limit
feeds = graph.get(path=hackathon_hacker_group_id + '/feed', fields='from,message', limit=9001)

In the normal case, after waiting a while, it should return an error. The reason behind is because the limit value is really high, thus making Facebook cry :~(

After setting the limit lower, it should be able to return a result.

However, since we need to gather 10000 feeds, thus need to tell the program to get the next page of the result until it reaches the goal number.

In the FacePy documentation , we can use the “page” boolean variable to return a generator instead, so it can keep yielding the result for us if there is next page.

Collecting all of the required options, and assign the program to print out certain amount of the result we want in the following.

from facepy import GraphAPI
from pprint import pprint as pp
# Initialize the Graph API with a valid access token (optional,
# but will allow you to do all sorts of fun stuff).
# Replace empty string with your user access token
user_access_token = ""
graph = GraphAPI(user_access_token)
# Locate the group id successfully
hackathon_hacker_group_id = '759985267390294'
# The amount it needs to fetch
amount = 9001
get_request = {'path':hackathon_hacker_group_id + '/feed','fields':'from,message','limit':1001, 'page':True}
res = graph.get(**get_request)
for i in res:
pp(i['data'] if amount > get_request['limit'] else i['data'][:amount])
amount -= len(i['data'])
if amount <= 0:
break

Finally, we would be able to collect the most recent 9001 feeds on Hackathon Hacker.

--

--