Open In App

Facebook API | Set-2

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite – Facebook API | Set-1
In this article, we will be discussing the implementation details of the Facebook API for Python Platform. Let’s begin with downloading Facebook Client for Python. Use following python command to download Facebook Client- 
 

pip install facebook-sdk 

Upon successful download you will see a screen like this- 
 

Now, let’s start with the methods that are provided with the facebook module for Python. We will be discussing the following methods in this article – 
 

  1. get_object
  2. get_objects

 

get_object:

This method returns the information as dict associated with the object determined by the object ID. 
 

Parameters: 
id: A string that is a unique ID for the particular Object. 
agrs(optional): agrs are to be passed as query params.

Example#1: 
Get details about the user (In this case, we are using ‘me’ parameter to display details from my account only). 
 

Python3




import json
import facebook
  
def main():
    token = "Please replace this line with your access token"
    graph = facebook.GraphAPI(token)
    profile = graph.get_object('me', fields ='first_name, gender, birthday, email'
     
    # return desired fields
    print(json.dumps(profile, indent = 4))
  
if __name__ == '__main__':
    main()



Example#2: 
Get Details about a Page (whether the user is the admin of the page or it has permission to manage the page). Before proceeding further Let us first see how to determine the Page ID. Please follow the steps below: 
 

  1. Click on the down arrow on the right hand side and Navigate to the page for which you want to determine the Page ID. 
     

  1. Click on the Page Name to go to the page.
  2. Click on See More Option on the Left hand side on the Page below the Page Profile Picture. After clicking you will see the Option “About” 
     

  1. Click on “About” Option. You will see the details of your page. Scroll Down to the Bottom and You will find your Page ID.

Now moving to the Python code to find out details of a Page. 
 

Python3




import json
import facebook
  
def main():
    token = "Please replace this line with your access token"
    graph = facebook.GraphAPI(token)
    page = graph.get_object(id ='PAGEID', fields ='about, can_post, category')
  
    # return desired fields
    print(json.dumps(page, indent = 4))
  
if __name__ == '__main__':
    main()


This code prints the basic information about the page(about), whether the requesting user is allowed to post anything on this page or not(can_post) and the Category of the Page(category). You can have the complete list of the fields from Page Reference 
 

Example#3: Get details about a Post: message and attachments
Lets first follow the steps below to find the Post ID for a Facebook Post. 
 

  1. On any Post, click on the date and time mentioned below the user name. 
     

  1. The post will open in a New Tab.
  2. Numerals in the last after the last slash (“/”) constitute your Post ID. 
     

Follow the steps below to find the User ID. 
 

  1. Go to Your Profile Page and right click on your profile Picture. Select the Option “Copy link address”
  2. Open Notepad and Press Ctrl+V or right click and select Paste option.
  3. A URL will be Pasted. User ID is mentioned in the end after the “referrer_profile_id”. 
     
https://www.facebook.com/photo.php?fbid=913492355516001&set=a.187231114642052&type=3&source=11&referrer_profile_id=100000677755756
  1.  

Code to find details of a Post. 
 

Python3




import json
import facebook
  
def main():
    token = "Please replace this line with your access token"
    graph = facebook.GraphAPI(token)
    post = graph.get_object(id ='USERID_POSTID', fields ='message, attachments{description}')
    # return desired fields
    print(json.dumps(post, indent = 4))
  
if __name__ == '__main__':
    main()



You can have the complete list of the fields from Post Reference.
 

get_objects:

This methods returns all objects from the Facebook Social Media Graph as dict. Each ID mentioned in the field list maps to an object.
 

Parameters: 
ids: A list containing IDs for multiple objects/resources 
agrs: This is optional and if mentioned need to passed as query parameters.

Example#1: Get the creation time of two different Posts. 
 

Python3




import json
import facebook
  
def main():
    token = "Please replace this line with your access token"
    graph = facebook.GraphAPI(token)
    post_ids =["USERID_POSTID# 1", "USERID_POSTID# 2"]
    posts = graph.get_objects(ids = post_ids, fields ='created_time')
  
    # print creation time of the two posts.
    print(json.dumps(posts, indent = 4))
  
if __name__ == '__main__':
    main()



Example#2: Get Comments Count of two different Photos.
Follow the steps below to find the Photo ID on Facebook. 
 

  1. Click on the Photo on Facebook for which you want to identify the Photo ID.
  2. Find “photo.php” in the Web address area.
  3. Look for “fbid” followed by a number just after the “photo.php” portion of the URL.
  4. The number that appears between the “fbid” tag and the “&” symbol is the photo ID number.

 

Python3




import json
import facebook
  
def main():
    token = "Please replace this line with your access token"
    graph = facebook.GraphAPI(token)
    photo_ids =["USERID_PHOTOID# 1", "USERID_PHOTOID# 2"]
    photos = graph.get_objects(ids = photo_ids, fields ='comments.summary(true)')
  
    # print total comment count for each photo
    print(json.dumps(photos, indent = 4))
  
if __name__ == '__main__':
    main()



References: 
 

  1. https://facebook-sdk.readthedocs.io/en/latest/api.html
  2. https://developers.facebook.com/docs/graph-api/reference

 



Last Updated : 30 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads