Facebook’s Graph API allows developers to access and manage Facebook pages programmatically, and you can do many things with it, including making posts, getting audience insights, managing ads, and much more without needing to visit Facebook at all. While there are many ways to do it, it’s especially easy with Python. This guide will show you how to use Python and the Graph API from Meta to get a list of the pages you can post to and their ID.
Prerequisites
Before you can begin this guide, you will need to have a Facebook developer account and a Facebook app with the access token to go with it. If you need help creating the Facebook app, we have a guide to help you get going, and you can pick it up right here when you finish. We also have a long list of Python projects that you should check out, especially if you’re just getting started on programming.
Set up your Python environment
First, you need to install the requests library, which will help us interact with the Facebook Graph API.
Run this command at the command prompt to install the library at your earliest convenience:
pip install requests
Write the Python Script
With the library installed, we can begin working on the script. We’ll explain each part of the code and post the entire thing at the end so you can copy and paste the code into a notepad if you want to.
Prompt for Access Token
The first thing we’ll do is write some code to prompt the user for the access code. This addition will let you use the same script to check any access token:
def get_access_token():
"""
Prompt the user to enter their Facebook User Access Token.
"""
return input("Please enter your Facebook User Access Token: ")
Fetch the Pages You Manage
Once we have the access token, we can use the /me/accounts Graph API endpoint to fetch the pages that the user manages. The app creation documentation provides more of these endpoints.
def get_pages(access_token):
"""
Fetch the list of pages the user has access to using the Graph API.
"""
url = "https://graph.facebook.com/v17.0/me/accounts"
params = {
'access_token': access_token
}
# Make a request to the Graph API to get the list of pages
response = requests.get(url, params=params)
if response.status_code == 200:
pages = response.json().get('data', [])
return pages
else:
print("Error fetching pages:", response.status_code, response.text)
return []
Check Permissions for Posting
After fetching the list of pages, we need to check whether the user has permission to post to each page. The permissions we are looking for are CREATE_CONTENT and MANAGE, and we’ll use the following code to see which pages have them.
def check_pages_with_post_permission(pages):
"""
Check which pages the user can post to by looking at the permissions.
"""
postable_pages = []
for page in pages:
perms = page.get('perms', [])
if ‘CREATE_CONTENT’ in perms or ‘MANAGE’ in perms:
postable_pages.append({
'id': page.get('id'),
‘name’: page.get(‘name’)
})
return postable_pages
Put It All Together
In the main part of the script, we combine all these functions. It will prompt for the access token, fetch the pages, check which pages you can post to, and display the results. The script will end when you press Enter.
def main():
"""
Main function to run the script.
"""
# Get the access token from the user
access_token = get_access_token()
# Fetch the list of pages
pages = get_pages(access_token)
if pages:
print("\nYou manage the following pages:")
for page in pages:
print(f"- {page['name']} (ID: {page['id']})")
# Check which pages have the permission to post
postable_pages = check_pages_with_post_permission(pages)
if postable_pages:
print("\nYou can post to the following pages:")
for page in postable_pages:
print(f"- {page['name']} (ID: {page['id']})")
else:
print("\nYou do not have permission to post to any of the listed pages.")
else:
print("No pages found or an error occurred.")
# Wait for the user to press Enter before exiting
input("\nPress Enter to end the script...")
# Run the main function
if __name__ == "__main__":
main()
Full Python Script
Here is the complete script you can copy and paste into a text file. If you then rename that file with a .py extension (pagechecker.py) you can save it and double click to run it.
import requests
import json
def get_access_token():
"""
Prompt the user to enter their Facebook User Access Token.
"""
return input("Please enter your Facebook User Access Token: ")
def get_pages(access_token):
"""
Fetch the list of pages the user has access to using the Graph API.
"""
url = "https://graph.facebook.com/v17.0/me/accounts"
params = {
'access_token': access_token
}
# Make a request to the Graph API to get the list of pages
response = requests.get(url, params=params)
if response.status_code == 200:
pages = response.json().get('data', [])
return pages
else:
print("Error fetching pages:", response.status_code, response.text)
return []
def check_pages_with_post_permission(pages):
"""
Check which pages the user can post to by looking at the permissions.
"""
postable_pages = []
for page in pages:
perms = page.get('perms', [])
if ‘CREATE_CONTENT’ in perms or ‘MANAGE’ in perms:
postable_pages.append({
'id': page.get('id'),
‘name’: page.get(‘name’)
})
return postable_pages
def main():
"""
Main function to run the script.
"""
# Get the access token from the user
access_token = get_access_token()
# Fetch the list of pages
pages = get_pages(access_token)
if pages:
print("\nYou manage the following pages:")
for page in pages:
print(f"- {page['name']} (ID: {page['id']})")
# Check which pages have the permission to post
postable_pages = check_pages_with_post_permission(pages)
if postable_pages:
print("\nYou can post to the following pages:")
for page in postable_pages:
print(f"- {page['name']} (ID: {page['id']})")
else:
print("\nYou do not have permission to post to any of the listed pages.")
else:
print("No pages found or an error occurred.")
# Wait for the user to press Enter before exiting
input("\nPress Enter to end the script...")
# Run the main function
if __name__ == "__main__":
main()