Create a Python Script to Post on Facebook in 5 Minutes

SOPA Images/GettyImages
facebooktwitterreddit

If you regularly post to Facebook, one of the best ways to improve productivity is with an automatic post scheduler. One way to do that is to create a simple Python script that you can attach to a calendar or cron job. Let’s look at creating that script now.

Creating a Facebook app

Any Python script that we create to post on Facebook will use a Facebook app to do so, and you will need to make it before we can start with the Python script. Luckily, it’s not hard.

  • At the top of the page, you will see Create app.
  • Pressing that will take you to a new screen where you will choose not to connect a business, then another page where you will select Other.
  • On the next page, you will choose Business.
  • Next, you will need to choose a name for your app. This is an important decision because when you make a post on Facebook, it will say “published by your-app-name” at the top of every post. You will also need to input your email and leave it without a business profile.
  • After you enter your password, you will be on the add products page. Click on the Set up button for the Instagram button, but you won’t have to do anything. On the left-hand side of the screen, click Add Product to get back to the Add Products page and click Set up for Facebook Login for Business. At the bottom of the next page, you will need to hit Save.

Get your page ID

With the app created, we can use it as a go-between between Python and Facebook, but to make a post, we will need our page ID.

  • Click tools at the top of the page, then click Graph API Explorer.
  • On the right-hand side of the screen, use the Pull Down under Meta App to select the app you just created if it isn’t already selected. Under that, it should say Get Token for User or Page, so leave that for now.
  • Under Permissions, you will need to use the dropdown box to look under Events Groups Pages to select pages_manage_posts, which will allow you to make Facebook posts. You can also select any other permissions you think you might want to make use of in the future.
  • Click the blue Generate Access Token near the top of the screen. It will ask you which pages you want the app to have access to, or you can choose all of them if you have more than one. However, you must pay attention to the number under the page name. That is your page ID, and you will need it in your script, so copy it and save it.
  • Once you select which page you will post to, the next page will allow you to review your app, so ensure it can create and manage content where you need it to and save it.

Get your page access token

  • Click tools at the top of the page, then click Graph API Explorer.
  • Make sure your app is selected under Meta App, and use the dropdown under User or Page to select the page you want to post under Page Access Tokens. Doing so will automatically change the token above the Get Access Token button, so you do not need to press it again.
  • Copy that code to get your page access token.

The Python Script

If you don’t know how to run this script and are just getting started with Python, check out our long list of tutorials.

You can copy and paste the following Python script into a text file and run it to make a post on the Facebook page you have the access code for:

First, install the Requests if you don’t already have it

pip install requests

Here is the complete code:

import requests

# Replace these with your actual access token and page ID or user ID

access_token = 'your_facebook_access_token'

page_id = 'your_page_or_user_id'

message = 'Hello, this is a test post from my Python script!'

# Define the URL for posting to the Facebook Graph API

post_url = f'https://graph.facebook.com/v17.0/{page_id}/feed'

# Parameters to be sent with the request

payload = {

    'message': message,

    'access_token': access_token

}

# Make the POST request to Facebook

response = requests.post(post_url, data=payload)

# Check if the post was successful

if response.status_code == 200:

    print('Post published successfully!')

else:

    print(f'Failed to post. Status code: {response.status_code}')

    print(f'Error: {response.text}')

To run this script, first change the code to include your page ID and access code where it says to do so.

What next?

  • Next, you will need to use the Access Token Debugger to get a long-lasting token, so you don’t need to keep refreshing it.
  • You will also likely change the message to a variable so it doesn’t stay the same.
  • You can write code to access a spreadsheet to create a long list of posts that the script goes through one by one.

Follow GeekSided to learn all of these things and more!

feed