Programatically Posting to Facebook’s API

Every time I get the bright idea to make a post that automates Facebook posting, I spend an hour re-learning how to access the API end point correctly.

So today, when I had the idea to make another app to post to Facebook, I spent another hour re-learn and figured I should just write it down somewhere and not forget. So here are some simple tips to making a Python Bot connect to your Facebook Page, and allow posting.

Create an app

The first thing your going to need to do is create an app. Even if it’s just for development purposes, you cannot access you Facebook pages without an app. So log into and create a new app. The form is pretty much a wizard, so just keep hitting next until you have created your app.

Get Your Access Token

Once you have your app associated with your page, and your personal Facebook account, visit to get your token. Here, you will see what looks like an odd form, but really its just a way for you to manually make requests to Facebook’s API.

The areas in red are what you want to focus on. From the top down, you have your Application, Access Token, Action Button, and Request Type options. For application, select the application you just made in the previous step.

Once you have the app selected, click on “Get Token”, and choose the page which you seek to post to through GraphAPI. Once your page, the page will kind of refresh with a new token.

Now, with your page access token, click the action button once more, and check “request publish_pages” permission.

Once again, go through the wizarding process and finally you will have a relevant Facebook token. Although you have a token, it doesn’t last long. So within the day, your access token will become expired. So the next step is to extend the token. So copy the long token and head over to

Triple click on this token, and copy it.

Extend Your Token

Once you’re on the token debugger page, paste your token into the field then click debug. You will get a new view telling you information about your token. Make sure your token scopes match the scopes you see in the photo below.

If everything looks good, note the expiration time of the token is set to 1 hour. This is hardly practical, so click the extend token option, and you will have two months to test with that token.

When you hit Extend Access Token, you will be prompted to put in your Facebook profile’s password. Once you do, you will be presented a message saying your token has been extended for two months.

Use token to publish to page

Finally, we can post to our Facebook page with the access token. I will write out a simple Python script to demonstrate how it works. One thing I want to say is, while there are SDK’s, for this snippet I will just utilize the ‘Requests’ library along with the endpoints provided in the Facebook API documentation.

import requests

access_token = 'EAAxxxxxxx'
page_id = "2784209xxxxxxxx"
message = "this is a test message"

url = "" 
url += page_id + "/feed?"
url += "message=" + message
url += "&access_token=" + access_token
# returns the following: is a test message&access_token=EAAxxxxxxx
r =

print r.text
# response object: {"id":"278420956190729_281120955920729"}

Now we check the page to confirm the message has been posted.


You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *