This space is available for advertisement. 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.

Related Articles:
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....
Python Script 8: Validating Credit Card Number - Luhn's Algorithm
Validating credit card number using Luhn' Algorithm, Verifying Credit and Debit card using python script, Python code to validate the credit card number, Luhn' algorithm implementation in Python...
Python Script 5: How to find most popular technologies on Stackoverflow
How to find most popular technology on stackoverflow by crawling the stackoverflow site using python. Using python beautifulsoup to crawl web pages on stackoverflow. Python code to crawl stackoverflow, crawling stackoverflow for tags, python script to fetch data from stackoverflow....
Uploading a file to FTP server using Python
Uploading files to FTP server using Python, Python script to connect to ftp server, Python code to login to FTP server and upload file, How to connect to FTP server using python code, ftplib in python, Get server file listing using ftplib in python...

2 thoughts on 'Automating Facebook Page Posts Using Python Script'
Swatantra :
Could you please post about python +robot framework.

Harini Lokku :
How can we upload images in the posts??
Admin :
Not sure. never worked. Will updates the article once I will do this.

Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

DigitalOcean Referral Badge

Get a .COM for just $5.98!

© 2021-2022 Python Circle   Contact   Sponsor   Archive   Sitemap   Partner Sites: PythonSnippets.Dev  99Dev.Tools