Using Facebook Graph Search through Python

A few years ago, Facebook deprecated its restful API and introduced the new Graph API. The new Graph API was launch after years of hard work, as Facebook engineers tried to bring in all the features of the existing API and promised to bring in more. There are different languages through which you can access Facebook graph search, but for this post, we will concentrate on using the Python version.

The OAuth Access Token

Every request in the Facebook Graph API is to be authenticated through a token, which is generated for every user uniquely, identifying the user and the application which was used to generate the token. The reason an application is associated with the token is to manage the permissions that the token should have. You can generate your token through some application or very easily from the Graph API Explorer. You need to specify what permissions you want to give to the token, and then generate a token.

The official Python SDK

Facebook has a Python wrapper over the Graph API and open sourced it on GitHub. It provides a number of built in functions to use. To initiate a Graph object, you would need an API token from the URL above.

graph = facebook.GraphAPI(access_token) # Initializing the object

profile = graph.get_object("me") # Extracting your own profile

graph.put_object("me", "feed", message="Posting on my wall!")

Using the SDK to login users to your site

Many websites have the feature of logging users in through facebook. In such a situation, Facebook returns a token on successful authentication, which has to be validated from the back end. Here’s how to do it through Python.

graph = facebook.GraphAPI(token) # After receiving it through Facebook

profile = graph.get_object("me") #Would return error if there is some problem with the token

email = profile['email'] # Get email

name = profile['name'] # Get name

In fact, the profile object contains a lot of information, including and not limited to page likes, unless you specified you just need a few. You can use this information to log people in to your site.

Common uses

There are many uses of the Graph API (not just using Python), which can be accessed making requests through specified URLs (all starting with graph.facebook.com and containing the token in the request).

Check if two people are friends

/user_id_1/friends/user_id_2/?access_token=<access_token>

 

In case the people are not friends, an empty response is shown as follows-

{
“data”: [

]
}

In case they are friends, the following response is shown.

{
"data": [
{
"name": "name",
"id": "id"
}
],
"paging": {
"next": "<url>"
}
}

Check if a user likes a page

Just like the above, the URL to access this info is as follows.

/user_id/likes/page_id/?access_token=<access_token>

You will get an empty response if the user doesn’t like the page.

An introduction to Facebook Query Language- FQL

FQL is an SQL like language to query the data that you receive through the Graph API. You can head over to the documentation for more information.

The following query gets the list of your friends.

“SELECT uid2 FROM friend WHERE uid1=me()”

To put it in a URL, you need to send the query as a get variable to graph.facebook.com/fql and the access token as another get variable. Obviously, all the spaces need to be replaced by “+”. Do note that while sending the access tokens, you need to put https.

.

The following query gets the data of the last 100 wall posts.

"SELECT post_id, actor_id, message FROM stream WHERE filter_key = 'others' AND source_id = me() LIMIT 100"

With this, we come to the end of the general overview of using the Facebook Graph API through Python. To explore it further, feel free to check out the documentation. Also, do leave a comment below if you have any issues.