Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically posting to facebook page with python

I want to make a script that lets me post to my facebook fan page (which I'm an admin of)

As far as I've seen, Most graph api examples are about making facebook apps in python and make them communicate with python, which is quite different from what I want.

Also The graph api requires the oauth token, which the documentation claims it's obtained by doing:

https://www.facebook.com/dialog/oauth? client_id=YOUR_APP_ID&redirect_uri=YOUR_URL

I think this implies:

a) I HAVE to create a facebook app for this, which I didn't think it was necessary (after all it's something that would require only my standard credentials and wouldn't be used by other people), but it is fine. I have an app created for this task.

b) I need an URL, which I don't have, because it's just a script.

Any ideas on where I should look for some info?

like image 849
Bruno Avatar asked Feb 23 '11 20:02

Bruno


People also ask

How do I automate Facebook with Python?

In order to log into Facebook using Python, you need to use Selenium (a web automation tool). Selenium can automate and control a browser and click, fill text, submit buttons that appear on various websites.


1 Answers

First You need to get your Facebook_App_Id and Facebook_App_Secret, from facebook which you will get when you register your app.

Then you include the needed urls.

redirect_client_url = 'http://your-redirect-url'
access_token_url = 'https://graph.facebook.com/oauth/access_token?client_id='+consumer_key+'&redirect_uri='+red   irect_client_url+'&client_secret='+consumer_secret+'&code='
scope = 'publish_stream,offline_access,user_birthday,email'
authorize_url = 'https://graph.facebook.com/oauth/authorize?client_id='+consumer_key+'&redirect_uri='+redirect_client_url+'&scope='+scope+'&display=touch'
user_info_url = 'https://graph.facebook.com/me?access_token='

Your consumer key and consumer secret are the Facebook app id and facebook app secret respectively.

You would basically get the access_token following the Oauth2.0 guidelines and store the access_token and fan page idsomewhere in your database. https://github.com/simplegeo/python-oauth2 is a good example on how to get the oauth token. Then when you try to post use the access token by using something like this.

post_data = {'access_token':access_token, 'message':'hey this is a test!'}
request_path = str(facebook_id)+'/feed'
post_data = urllib.urlencode(post_data)
response = urllib2.urlopen('https://graph.facebook.com/%s' % request_path, post_data)

This should work for posting to a user's wall. But i am sure posting to a facebook fan page should be something very similar.

like image 197
Milind Avatar answered Sep 28 '22 09:09

Milind