/  Technology   /  Access Data From Facebook Using Facebook Graph API and Python
Access Data From Facebook Using Facebook Graph API and Python

Access Data From Facebook Using Facebook Graph API and Python

What is Facebook Graph API?

The Graph API is the primary way to get data into and out of the Facebook platform. It’s an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

Let’s get started

  1. Open your browser and search for Graph API
  2. Then select the first link which is shown.

Access Data From Facebook Using Facebook Graph API and Python

This will take you to this page and then select the tools option and then select graph API explorer. It will open the graph API explorer.

You have to login with your Facebook id to get started. So, first login and then start the process.

Access Data From Facebook Using Facebook Graph API and Python

As you can see there is Access token option. This token is like your password which shows who are you so, keep it safe.

The above link you can see i.e. me?fields = id, name where me means you, fields means which fields you like to see here we can see id and name of the Facebook account we have signed it with.

When you click on Generate Access token button the submit gets enabled. When you click on the submit button you can see the fields you have entered. In this case it is id and name.

Access Data From Facebook Using Facebook Graph API and Python

At the bottom right there is an option called Add permission. Select the User data Permission option and then select the fields that you want to access such as birthday, gender, hometown etc.

Access Data From Facebook Using Facebook Graph API and Python

In this case I have selected birthday and gender option. First you have to enable it from Add permission and then on the Left side you can search and add the fields you want to access and then generate access token and click on submit to see the result.

Let’s see how we can request the Facebook server using python. So that we can access our data.

Syntax

import requests
token = “”
profile = 'https://graph.facebook.com/v7.0/me?access_token='+token
data = requests. get(profile)

Let’s understand what the above code means.

First you have to import requests module on to your IDE we are using Sypder you can use any IDE but make sure you have installed all the necessary modules before importing it into your IDE.

Now for requesting information you have to create an URL which is

‘https://graph.facebook.com/v7.0/me?access_token=’+token, v7.0 means the version I am currently using then me which will give me the profile information now access token. We have to provide an access token. token here is the string variable that we have created. You have to add that token with this URL. For this access token you have to go back to the Facebook Graph API site where we have started the Graph API explorer there you can see an Access token option where the URL is given you have to simply copy and paste that URL in to your IDE where we have created a token string variable.

data = requests. get(profile)

Here we are storing the data given by the Facebook inside this data variable by using requests. get(profile). Now run the code

Once you run the code just type data. text in your console window to see your id and name which will be given in the form of dictionary.

Output

Access Data From Facebook Using Facebook Graph API and Python

In this way we have seen how we can access data from Facebook using Facebook Graph API and python.

 

Leave a comment