Deal with Facebook activity using Python

Hey, guys, we are back again with some interesting and exciting packages/modules. Hope you people are enjoying the Tutorial, if you feel that any of our article or points are not up to mark please deliver us a comment and we will be extremely happy to work on it to improve. Let us begin of Topic of discussion now, in this article, we will learn about the usage of API’s Facebook activity using Python. Yes, Facebook API’s you heard it right and it’s really exciting to play with it as well as we can integrate this module in any of our applications.

Deal with Facebook Activity using Python

facebook activity using python

Hi, friends I know that the UI part of Facebook interaction is much interesting and fascinating, but if you are into python programming world then you would certainly want to know interacting with Facebook Activity using python. Python provides few libraries provide the interaction among them the most efficient one’s are facebook-SDK and facepy. In this article, I will cover the concepts of the facebook-SDK module.

To understand this module let us understand better what is GraphAPI.

“The Graph API is the primary way to get data in and out of Facebook’s social graph. It’s a low-level HTTP-based API that you can use to query data, post new stories, upload photos and a variety of other tasks that an app might need to do.”


facebook.GraphAPI is the client for Facebook Graph API. The common parameter taken is the access_token, this is a string that identifies a user, app or page and can be used by the app to make graph API calls.

Here is a simple syntax for it

>>> import facebook
>>> graph = facebook.GraphAPI(access_token=‘your_token’)

So to begin with you need to generate the access token for you account, you can get more knowledge on what is access token and how it is generated by going to this link ——> Click here

So after this point, you are authenticated to use the Graph API’s but you need to note that the access token expires very frequently, Access tokens on the web often have a lifetime of about two hours, but will automatically be refreshed when required. If you want to use access tokens for longer-lived web apps, especially server side, you need to generate a long-lived token. A long-lived token generally lasts about 60 days.

Let’s have a look at the method defined in this module


This method returns the given object from the graph as a dictionary. It takes a string as a parameter which is the unique ID for that particular resource.

take a look at these examples

>>> m=facebook.GraphAPI(“access_token")
>>> post = m.get_object(‘15568********557_15568********871’)
>>> print post
{'created_time': '2016-01-31T14:40:48+0000', 'message': 'Hello Folks!', 'from': {'name': 'Ankur Gupta', 'id': ‘14701***********’}, 'id': '15568********557_15568********871'}
>>> myself = m.get_object('me')
>>> print myself
{'name': 'Ankur Gupta', 'id': '14701***********'}

m.get_object(‘15568********557_15568********871’) — In this case I specified a post id and in response, i got an object which is a dict that gives me the related information.

m.get_object(‘me’) — In this case, I am trying to get information about myself, which in response returns me my name and my-unique ID



This method returns all connections for a given object as Dictionary. It takes 2 parameters ID and connection_name, ID concept remains the same but here connection_name is a string that specifies the connection or edge between objects e.g., feed, friends, groups, likes, posts. If left empty, get_connections will simply return the authenticated user’s basic information.

Consider the examples

>>> friends = m.get_connections('me',connection_name='friends')
>>> print friends
{'data': [], 'summary': {'total_count': 0}}

here I am trying to get my friends list, but since this is my test_account I don’t have anyone include in the friend list. But guys do hit the query to see the actual result.


Writes the given object to the graph, connected to the given parent. It takes 3 parameters parent_object, connection_name and message.

we know about connection_name, now we have a message as a parameter which is self-explanatory and parent_object which is the unique ID for that particular resource.

Once again lets understand it’s usage with small examples

>>> m.put_object(parent_object='me',connection_name='feed',message='')                                                                          {'id': ‘14701***********_15569**********9’}

In the above example, we are writing a small message ‘’ on the user’s wall I mean on my account wall, which returns me a unique post_id

In the above example I have set connection_name to ‘feed’ now if I set it to ‘comments’ we will be actually commenting on a post but intros case I need to change parent_object=‘me’ to parent_object=‘post-id’.


In case you want to comment on a post this method is just for that, it takes 2 parameters one is message and object_id which is the post_id. This method returns the unique ID for the comment on the post.

>> m.put_comment(object_id=‘14701***********_15569**********9’,message='I love Python')

{'id': ‘15569***********_15569**********7’}


Using this method we can like a given post or the comment for which we need to specify only it’s unique ID.

In the below example i have just like the recent comment in the previous example using the below code.

>>> m.put_like(object_id='15569***********_15569**********7')
{'success': True}

This returns the success message if the comment is liked successfully.


Deletes the object with the given ID from the graph. It takes a single parameter which is a Unique ID.

>>> m.delete_object(‘15569***********_15569**********7’)

Thank you guys for going through this small intermediate-level tutorial, I hope you have enjoyed and had fun with this module. Please give us your feedback so that we can improve the approach we are following to teach you the python stuff. I think you now know of API’s of facebook activity using python.

So in case of any queries or question do reach us, We will help you in best possible way to keep the things going in you favor, Thank you.


  1. Kamal thakur January 31, 2016
  2. Eddie March 20, 2016
  3. mulberry bayswater pris April 23, 2016
  4. Pi June 20, 2016