Update Facebook status using Facebook Python SDK

In this post I am going to show how you can update your Facebook status using python. The first and foremost thing to do is install Facebook for python. You can do this by downloading or cloning from https://github.com/pythonforfacebook/facebook-sdk. Installation is quite easy. Unzip the file and run 'python setup.py install'. To verify Facebook Python SDK is installed you can run "python -c 'import facebook'". This should do absolutely nothing. No errors. If you get any error then Facebook SDK is not installed.
Once Facebook SDK is installed, you can login to http://developers.facebook.com/. And create a new app. I believe this much you can do by yourself.

Once an app is created in Facebook. Now open Graph API Explorer  which should look like as follows:
Here you need to do is get Access Token by clicking the 'Get Access Token' button on the right top side. In the screenshot you can see the Access Token is displayed on the text field. Copy this that's what we are going to need here. If you are thinking of giving away this program to away then you should definitely follow some other way of authentication and generate Access Token based on authenticated users. But for here we have our Access Token. Now the script is quite simple which is as follows:

#!/usr/bin/python
import facebook
graph = facebook.GraphAPI('Access Token')
graph.put_object("me", "feed", message="Here goes my status")
And this simple script would update your facebook status. You can do other cools stuffs too. To get your profile you can do 
profile=graph.get_object("me")
you can get your friends list by
myfriends=graph.get_connection("me","friends")
Also you can embed links in you status:
graph.put_object("me", "feed", message="Here goes my status","link"="http://writephp.blogspot.com")
There are a lot more you can do with Facebook's Python SDK. I guess it was real helpful in getting started.

Comments

Post a Comment

Popular posts from this blog

Automate file upload in Selenium IDE

How To Install and Configure Nextcloud