How to use Facebook Graph API and extract data using Python

How to use Facebook Graph API and extract data using Python

How to use Facebook Graph API and extract data using Python. In this Python tutorial we are going to talk about Facebook Social Graph API and how you can use Facebook Social Graph API Explorer also we are going to use Python programming language for retrieving some data from Facebook. What Is Facebook Social Graph API?

Getting the Access Token:

To be able to extract data from Facebook using a python code you need to register as a developer on Facebook and then have an access token. Here are the steps for it.

  1. Go to link developers.facebook.com, create an account there.
  2. Go to link developers.facebook.com/tools/explorer.
  3. Go to “My apps” drop down in the top right corner and select “add a new app”. Choose a display name and a category and then “Create App ID”.
  4. Again get back to the same link developers.facebook.com/tools/explorer. You will see “Graph API Explorer” below “My Apps” in the top right corner. From “Graph API Explorer” drop down, select your app.
  5. Then, select “Get Token”. From this drop down, select “Get User Access Token”. Select permissions from the menu that appears and then select “Get Access Token.”
  6. Go to link developers.facebook.com/tools/accesstoken. Select “Debug” corresponding to “User Token”. Go to “Extend Token Access”. This will ensure that your token does not expire every two hours.

Python Code to Access Facebook Public Data:

Go to link https://developers.facebook.com/docs/graph-api if want to collect data on anything that is available publicly. See https://developers.facebook.com/docs/graph-api/reference/v2.7/. From this documentation, choose any field you want from which you want to extract data such as “groups” or “pages” etc. Go to examples of codes after having selected these and then select “facebook graph api” and you will get hints on how to extract information. This blog is primarily on getting events data.

First of all, import ‘urllib3’, ‘facebook’, ‘requests’ if they are already available. If not, download these libraries. Define a variable token and set its value to what you got above as “User Access Token”.

token= ‘aiufniqaefncqiuhfencioaeusKJBNfljabicnlkjshniuwnscslkjjndfi’
Getting list of Events:

Now to find information on events for any search term say “Poetry” and limiting those events’ number to 10000:

graph = facebook.GraphAPI(access_token=token, version = 2.7)
events = graph.request(‘/search?q=Poetry&type=event&limit=10000’)

This will give a dictionary of all the events that have been created on Facebook and has string “Poetry” in its name. To get the list of events, do:

eventList = events[‘data’]
Extracting all information for a event from the list of events extracted above:

Get the EventID of the first event in the list by

eventid = eventList[1][‘id’]

For this EventID, get all information and set few variables which will be used later by:

event1 = graph.get_object(id=eventid,
 fields=’attending_count,can_guests_invite,category,cover,declined_count,description,end_time,guest_list_enabled,interested_count,is_canceled,is_page_owned,is_viewer_admin,maybe_count,noreply_count,owner,parent_group,place,ticket_uri,timezone,type,updated_time’)
attenderscount = event1[‘attending_count’]
declinerscount = event1[‘declined_count’]
interestedcount = event1[‘interested_count’]
maybecount = event1[‘maybe_count’]
noreplycount = event1[‘noreply_count’]

Getting the list of all those who are attending an event and converting the response into json format:

attenders = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/attending?access_token="+token+”&limit=”+str(attenderscount)) 

attenders_json = attenders.json()

Getting the admins of the event:

admins = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/admins?access_token="+token)

admins_json = admins.json()

And similarly you can extract other information such as photos/videos/feed of that event if you want.

Go to https://developers.facebook.com/docs/graph-api/reference/event/ and see “Edges” part in the documentation. See image

Now, let’s say, you want to have a list of all those who are interested in the event, click on ‘interested’ green word here. This will open up a new page:

Select ‘Graph API Explorer’ here. This will open a new page:

Here, in place of {event-id}, put the id of the event, like this:

Hit submit. Also, on the same page, you will find below ‘get code’ option

Select it to see the code. Select ‘curl’ in the pop up that appears and then get the same output in the python code, write it with requests.get as it has been shown in above examples.

Hope this helps those who are beginning to work with facebook graph API. I will be happy to hear your suggestions/questions/feedback.

If you found this blog of any value to you and if you are into cryptocurrencies and if you are generous, consider sending some ripples to the following address:

Destination Tag: 5973413

Wallet Address: rLdinLq5CJood9wdjY9ZCdgycK8KGevkUj

There are too many ifs in the above statement. Send only if all the three are true! Cryptocurrencies FTW!

Top Python Development Companies | Hire Python Developers

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When you look for the developer in hurry you may forget to take note of review and ratings of the company's aspects, but we at TopDevelopers have done a clear analysis of these top reviewed Python development companies listed here and have picked the best ones for you.

List of Best Python Web Development Companies & Expert Python Programmers.

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Hire Now: https://bit.ly/394wdOx

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company