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 developer.facebook.com


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 10: Collecting one million website links
Collecting one million website links by scraping using requests and BeautifulSoup in Python. Python script to collect one million website urls. Using beautifulsoup to scrape data...
Python Script 1: Convert ebooks from epub to mobi format
Python script to convert the ebooks from one format to another in bulk. Automated book conversion to kindle format. Free kindle ebook format conversion....
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 9: Getting System Information in Linux using python script
Finding linux system information like processor details, memory usage and average load using python script. Python program to find Ubuntu information....

0 thoughts on 'Automating Facebook Page Posts Using Python Script'
Leave a comment:


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


SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.



Recent Posts:






© pythoncircle.com 2018-2019