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.
One way to improve productivity if you regularly post to Facebook 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.
1. Go to the Meta Developers site and log in.
2. Click on “My Apps” in the top navigation bar.
3. Click on “Create New App.”
4. Select “Manage Page” as the app type and click on “Continue.”
5. Enter a display name for your app and click on “Create App.”
6. Click on “Skip Quick Start” and then on “Settings.”
7. In the left sidebar, click on “Basic Settings.”
8. Under “App Domains,” enter the domain name of the website or server that will be hosting your Python script. For example, if your script will be hosted on your personal website at the URL “https://example.com/script.py,” then you would enter “example.com” here.
9. Click on “Save Changes.”
10. Next, we need to add our Facebook page to the app. In the left sidebar, click on “Pages.”
11. Click on the “Add Page” button and select the page that you want to give your app permission to post on.
12. Click on the “Assign” button and then on the “Done” button.
13. Finally, we need to generate an access token for our app. In the left sidebar, click on “App Tokens.”
14. Click on the “Generate New Token” button and select “User Access Token” from the dropdown menu.
15. Click on the “Generate” button and copy the access token that is displayed. You will need this token later when we configure our Python script.
## 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.
1. Go to the Facebook page that you want to post on.
2. In the URL address bar, look for the string of numbers that appears after “facebook.com/.” This is your page ID. For example, if the URL of your page is “https://www.facebook.com/geeksides/,” then your page ID is “geeksides.”
## Get your page access token
The final thing we need before we can write our Python script is our page access token.
1. Go to the Graph API Explorer site and log in.
2. In the left sidebar, click on “User Access Token.”
3. Select the page that you want to get the access token for from the dropdown menu.
4. Click on the “Generate Access Token” button and copy the access token that is displayed. You will need this token later when we configure our Python script.
## 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:
```python
# 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. Then, save the file with a .py extension (e.g.,
The above is the detailed content of How to Auto Post to Facebook Using Python. For more information, please follow other related articles on the PHP Chinese website!