python and the facebook api

| categories: python, programming

to run this example, you need at first a facebook app. this video shows the simple setup. pick the App ID and App Secret from there and insert it in the python code below.

the program returns all comments from a single facebook post. you can limited the request and you can change the facebook post ID. the graph api explorer is the perfect starting point to learn more about the fb api.

the last part is for error handling and simple json parsing to get all the interesting information.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from json import loads
from urllib2 import Request, urlopen, URLError, HTTPError

APPID = "XXXXXXXXXXXXXXXXXX"
APPSECRET = "XXXXXXXXXXXXXX"
FBPOSTID = "588013424601016"
LIMIT = 3000
GRAPH_URL = "https://graph.facebook.com/%s/comments?access_token=%s|%s&limit=%d" % (
FBPOSTID,
APPID,
APPSECRET,
LIMIT)
try:
response = urlopen(GRAPH_URL)

except HTTPError as e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code

except URLError as e:
print 'We failed to reach a server.'
print 'Reason: ', e.reason

else:
decoded = loads(response.read())
for item in decoded['data']:
print '%s,%s,%s' % (
item['created_time'],
item['from']['name'],
item['message'])

Read more




Next Page ยป