Facebook API | Set-2

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.
args(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).

filter_none

edit
close

play_arrow

link
brightness_4
code

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()

chevron_right


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.
  2. Click on the Page Name to go to the page.
  3. 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”
  4. 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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()

chevron_right


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.
  2. The post will open in a New Tab.
  3. 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

Code to find details of a Post.

filter_none

edit
close

play_arrow

link
brightness_4
code

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()

chevron_right


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
args:This is optional and if mentioned need to passed as query parameters.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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()

chevron_right


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.
filter_none

edit
close

play_arrow

link
brightness_4
code

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()

chevron_right


References:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.