Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
Automating Facebook page posts using python script

Automating your Facebook page post is extremely easy. All you need is Graph API token and Python code.

Getting Page Access Token:

Create a facebook app on

facebook developer app

Change the app status to live to make it public. 

Go to tools -> Graph Api explorer.

graph api explorer

Select your application.

Select page access token and then select the page name on which you want to post message using python code.

Select the 'i' icon at the start of access token.

page access token facebook

Click on 'Open in Access Token Tool'.

On the next page, click on on 'Extend Access Token'.

page access token facebook extended

Copy this access token and keep in config file. Remember not to commit this token into git.

Python Code:

It is recommended that you create a virtual environment using python3.

Activate the virtual environment and install the facebook-sdk==2.0.0 package.

source path-to-venv/bin/activate
pip install facebook-sdk==2.0.0

Use below code to post on your page on behalf of your facebook app your created above.

import facebook

page_access_token = "YOUR TOKEN GOES HERE"
graph = facebook.GraphAPI(page_access_token)
facebook_page_id = "67509909999999"
graph.put_object(facebook_page_id, "feed", message='test message')

After executing above script, verify on your page if post has been published or not.

automated post fb page

You can schedule a cron to post the status on facebook page regularly. 

Remember to renew the page access token before it expires.

Feel free to comment in case of any issue.

Host your Django App for Free.

2 comments on 'Automating Facebook Page Posts Using Python Script'
Login to comment

Swatantra Dec. 2, 2019, 12:25 p.m.
Could you please post about python +robot framework.
Harini Lokku March 21, 2020, 5:47 a.m.
How can we upload images in the posts??

Related Articles:
Python program to convert Linux file permissions from octal number to rwx string
Python program to convert Linux file permissions from octal number to rwx string, Linux file conversion python program, Python script to convert Linux file permissions...
Python Script 17: Setting bing image of the day as desktop wallpaper
Python Script to Set bing image of the day as desktop wallpaper, Automating the desktop wallpaper change to bing image of day using python code, changing desktop wallpaper using python, downloading an image using python code, updating the desktop wallpaper daily using python...
Python Script 16: Generating word cloud image of a text using python
word cloud python, generating a word cloud of a text using python code, python code to generate a word cloud image of a text. Using word frequencies to generate a word cloud image using a python script...
Python Script 13: Generating ascii code from Image
Generating ascii art from image, converting colored image to ascii code, python script to convert image to ascii code, python code to generate the ascii image from jpg image....
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap