How to Automate Facebook Posts to Page using Python and Graph API

Published May 08, 2024

Default First Image

I maintain a few Facebook Pages where I post useful content to GRE test takers and others. It is more efficient to have a script automate this process. Facebook has good documentation on how to automate posts on your personal page, but the documenation is vague on how to do it for a Business Page. This blog post will show you how to do it.

Create a Facebook App

Assuming that you already have a Facebook Page, login to Facebook and go to https://developers.facebook.com/apps/ and create a Facebook app for your page.

At this URL https://developers.facebook.com/apps/PAGE_ID/settings/basic/ where PAGE_ID is your Facebook Page's ID, you will see your App ID and App Secret. Do not share these with anyone.

Generate Access Token in Facebook's Graph API Explorer

Go to the Facebook's Graph API Explorer webpage at https://developers.facebook.com/tools/explorer/.

You will need an access token. On the right side, click on User or Page.

In the resulting drop down, look at Page Access Tokens and select the Faceook Page for which you want to obtain the access token.

Then, click on Generate Access Token. Copy and save that access token temporarily in Notepad or Sublime Text.

This access token has a life time of only 60 days. We will extend it to indefinite in the next step.

Extend the Access Token's lifetime

Go to the Access Token Debugger page at https://developers.facebook.com/tools/debug/accesstoken/.

Select the latest API version. It is currently 19.0.

Then, paste the access token into the textbox that says "Enter access token to debug" and click on the Debug button.

At the bottom of the page, click on Extend Token. This will extend the access token's lifetime from 60 days to indefinitely.

Verify that the Access Token has a long lifetime

Go to this link. Replace PAGE_ID and LONG_ACCESS_TOKEN with the actual values.

https://graph.facebook.com/PAGE_ID?fields=access_token&access_token=LONG_ACCESS_TOKEN

If you look at the Expires field, it will show never.

Python program to automate posting to your Facebook Page

Copy / paste this Python code into a file called post_to_facebook_page.py. Or just download it here.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Make Automated Posts to Facebook Page
Written by Arul John
Blog Post: https://aruljohn.com/blog/python-automate-facebook-posts/
"""

import requests

# Explorer
# https://developers.facebook.com/tools/explorer

# Variables
version = 'v19.0'

# Your App information
access_token = ''   # Add your Access Token
app_id = ''         # Add your App ID
app_secret = ''     # Add your App secret
page_id = ''        # Add your Facebook Page ID

# Get long token
print(f'''
curl -i -X GET "https://graph.facebook.com/{version}/oauth/access_token?grant_type=fb_exchange_token&client_id={app_id}&client_secret={app_secret}&fb_exchange_token={access_token}"
''')

# To post text with a URL
my_url = ''             # Add your URL
message = 'Hello World' # Update this with the text in your Facebook post

baseurl = f'https://graph.facebook.com/{version}/{page_id}/feed'
payload = {
    'message': message,
    'link': my_url,
    'access_token': access_token
}
# Make Facebook post on your Page
res = requests.post(baseurl, data=payload, timeout=10)
print(res.text)

# To post an image with a caption
my_image_url = ''       # Add the URL to the image you want to post
message = 'Hello World' # Update this with the text in your Facebook post

baseurl = f'https://graph.facebook.com/{version}/{page_id}/photos'
payload = {
    'message': message,
    'url': my_image_url,
    'published': True,
    'access_token': access_token
}
# Make Facebook post on your Page
res = requests.post(baseurl, data=payload, timeout=10)
print(res.text)

Please do not copy / paste it as-is. You have to update the variable values in the script. The comments show where you have to enter the values.

Conclusion

Hopefully, this blog post should help you with automating your Facebook Page posts. If you want to extend this program to make it read text from a database or select a random image from the web, let me know how it goes.

As always, Facebook API keeps changing with or without notice and things break every few months. This blog post will be updated everytime there is a change. So you may want to bookmark it, just in case.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: May 08, 2024.     This post was originally written on March 12, 2024.