The Facebook API platform helps third-party developers like us to create our own applications and services that access data on Facebook.
Let us draw the Facebook data using the Facebook API. Facebook provides two types of APIs; that is, Graph API and Ads API. Graph API is a RESTful JSON API with which we can access the different resources from Facebook like statuses, likes, pages, photos, and so on. The Ads API basically deals with managing access to add campaigns, audiences and so on.
In this chapter, we are going to use the Facebook Graph API to interact with Facebook. It is named after its manner of representation with nodes and edges. The nodes represent the things, which means a user, a photo, a page; and the edges represent the connection between the things; that is page's photos, photo's comments.
To get started with the Facebook API, we need an opaque string called access token which is used by Facebook to identify a user, app, or page. It is followed by the steps of obtaining a key. We will be sending almost all our requests to the API at graph.facebook.com
except the video upload stuff. The procedure to send a request takes place using the unique id of the node in the following way:
GET graph.facebook.com/{node-id}
And in the same way, we can POST in the following way:
POST graph.facebook.com/{node-id}
The tokens of Facebook API are portable and can be used to make calls from a mobile client, a web browser or from a server.
There are four different types of Access tokens:
In this tutorial, we will be using the App access token which consists of App Id and App Secret to get access to the resources.
Follow the below steps to obtain an App access token:
That's all; obtaining a key is that simple. We don't need to create any authentication request to send messages, as opposed to how it is on Twitter. The App Id and App Secret are enough to give us permission to access the resources.
We can access the current user profile of the person who is logged into the site, using the API URL https://graph.facebook.com/me
with a GET request. We need to pass the previously obtained access token as a parameter, while we are making any Graph API call using requests.
Firstly, we need to import the requests module and then we have to store the access token into a variable. The process works in the following way:
>>> import requests >>> ACCESS_TOKEN = '231288990034554xxxxxxxxxxxxxxx'
In the next step, we should send the required graph API call, in the following way:
>>> me = requests.get("https://graph.facebook.com/me", params={'access_token': ACCESS_TOKEN})
Now, we have a requests.Response
object called me
. The me.text
returns a JSON response string. To access various elements (example, id
, name
, last_name
, hometown
, work
) of the retrieved user profile, we need to convert the json
response
string into a json object
string. We can achieve this by calling the method me.json()
. The me.json.keys()
results all the keys in the dictionary:
>>> me.json().keys() [u'website', u'last_name', u'relationship_status', u'locale', u'hometown', u'quotes', u'favorite_teams', u'favorite_athletes', u'timezone', u'education', u'id', u'first_name', u'verified', u'political', u'languages', u'religion', u'location', u'username', u'link', u'name', u'gender', u'work', u'updated_time', u'interested_in']
A user's id
is a unique number which is used to identify the user on Facebook. We can access the current profile ID from the user profile in the following way. We'll use this ID in the subsequent examples to retrieve the current user's friends, feed and albums.
>>> me.json()['id'] u'10203783798823031' >>> me.json()['name'] u'Bala Subrahmanyam Varanasi'
Let us gather the friends list of a specific user. To achieve this, we should make an API call to https://graph.facebook.com/<user-id>/friends
, and replace the user-id
with the value of user's ID.
Now, let us obtain the friends list of the user id that we retrieved in the former example:
>>> friends = requests.get("https://graph.facebook.com/10203783798823031/friends", params={'access_token': ACCESS_TOKEN}) >>> friends.json().keys() [u'paging', u'data']
The response for the API call contains a JSON object string. The friend's information is stored in the data
attribute of the response json
object, which is a list of friend objects containing friends' IDs and names as keys.
>>> len(friends.json()['data']) 32 >>> friends.json().keys() [u'paging', u'data'] >>> friends.json()['data'][0].keys() [u'name', u'id']
In order to retrieve the feed of posts which includes status updates and links published by the current user, or by others on the current user's profile, we should use the feed parameter in the request.
>>> feed = requests.get("https://graph.facebook.com/10203783798823031/feed", params={'access_token': ACCESS_TOKEN}) >>> feed.json().keys() [u'paging', u'data'] >>> len(feed.json()["data"]) 24 >>> feed.json()["data"][0].keys() [u'from', u'privacy', u'actions', u'updated_time', u'likes', u'created_time', u'type', u'id', u'status_type']
In the preceding example, we sent a request to get the feeds of a specific user with user ID 10203783798823031
.
Let us access the photo albums created by the current logged-in user. It can be achieved in the following way:
>>> albums = requests.get("https://graph.facebook.com/10203783798823031/albums", params={'access_token': ACCESS_TOKEN}) >>> albums.json().keys() [u'paging', u'data'] >>> len(albums.json()["data"]) 13 >>> albums.json()["data"][0].keys() [u'count', u'from', u'name', u'privacy', u'cover_photo', u'updated_time', u'link', u'created_time', u'can_upload', u'type', u'id'] >>> albums.json()["data"][0]["name"] u'Timeline Photos'
In the preceding example, we sent a request to graph API to get access to the albums of the user with user-id
10203783798823031
. And then we tried to access the response data through JSON.
3.12.166.255