Working with Facebook Graph API
As of now, you must be familiar with the word API. Nowadays social APIs are playing an important role to gather information about an individual or an organisation or almost anything. So we will be looking at implementing Facebook Graph API using python to mine some data from Facebook.
If you go to https://developers.facebook.com/docs/, you will find the languages for which facebook provides API support. But, facebook doesn’t provide an official client for python. So, we will be using Facebook SDK instead for the purpose. Before we get started to mine(extract or data mining) some data let's first install the Facebook SDK for python. It's quiet simple:
sudo pip install facebook-sdk
Before jumping into data mining from Facebook, there are some requirements which we must fulfill beforehand. We need an Access Token
to access any data from the social networking site and gather information. So, what is an Access Token
?
To keep it simple, let's say it's a kind of access code or permission which Facebook(or any other website) provides, so that you can access the data as a legitimate user and they can keep track of it.
An access token is an object that describes the security context of a process or thread. The information in the token includes the identity and privileges of the user account associated with the process or thread.
You need to have a Facebook account to request an access token. Now follow the following steps:
- Login to your account.
- Visit https://developers.facebook.com/
- Now, on the top right section. Click on Get Started section. And follow the formalties and proceed.
- Now, select a display name for your app and click on Create App ID.
- After your
App ID
is generated visit https://developers.facebook.com/tools/explorer/
- Now, click on Get Token → Get User Access Token. If you have multiple apps than you might need to select for which app you want to generate the token.
- Now select User Data Permissions and click Get Access Token
- Your token will be generated. Copy that to clipboard. It is advised not to share this access token. And if you are using it in a program, do not use directly. It is advised to save this token in a separate file and take input from there or set an enviroment variable like
$ set FACEBOOK_TEMP_TOKEN = "YOUR_TOKEN"
.
But, since these token expire after some time you need to update it again and again.