How to Easily Test Facebook Access Tokens with Python: Step-by-Step Guide

SOPA Images/GettyImages

With Python, you can write a simple script for testing the access token that you need to use the API to make posts, check stats, and more. Whether you are just learning how to use the Facebook API or you generate a lot of access tokens, keep reading for a simple script you can copy and paste that will tell you if the access token is still active or expired.

The Python Script

We’ll explain the code so you understand what’s happening, and then we’ll give you the complete code at the end. If you’re new to Python, check out our complete collection of guides and tutorials, including one that shows you how to get the access token we are testing here.

Import the required library.

We’ll use the requests library to send HTTP requests to the Facebook Graph API. First, make sure you have the requests library installed by running this command in your terminal:

pip install requests

Then, you can import it at the start of your script:

import requests

Ask the user to input their access token

So we can reuse the script to check many tokens, we’ll add this section, which asks the user to input the token instead of hard coding it.

# Prompt the user to enter their Facebook access token

access_token = input("Please enter your Facebook access token: ")

Build the URL for the Facebook Graph API request

Next, we put the link to the Facebook API into the script, which we will use to send a request to the Facebook Graph API to get the user’s basic profile information. We’ll request basic user information by using the /me endpoint.

# The URL to test the access token, retrieving user info

url = f’ https://graph.facebook.com/v12.0/me?access_token={access_token}’

Send the request to Facebook.

Next, we’ll send the HTTP request to Facebook using the requests.get() method. This line sends the GET request to the URL we just created and stores the response in the variable response.

# Requesting the Facebook Graph API

response = requests.get(url)

Check if the token is valid

After sending the request, we check the status of the response to determine if the token is valid. If the request returns a 200 status code, the token is valid, and we can print the user’s information.

# Check the status of the response

if response.status_code == 200:

    # Token is valid; print user information

    user_info = response.json()

    print("Access token is valid.")

    print("User information:", user_info)

Handle invalid tokens or errors.

If the access token is invalid, expired, or if there’s an error, the script will return a message indicating the issue. We handle this with the else statement below:

else:

    # Invalid token or other error

    print(f"Failed to validate access token. Status Code: {response.status_code}")

    print("Response:", response.json())

Prompt the user to press “Return” to exit the script

Next, we’ll pause the script and ask the user to press “Return” to exit to give them time to read the response before the script ends.

Complete script

import requests

# Prompt the user to enter their Facebook access token

access_token = input("Please enter your Facebook access token: ")

# The URL to test the access token, retrieving user info

url = f'https://graph.facebook.com/v12.0/me?access_token={access_token}'

# Making a request to the Facebook Graph API

response = requests.get(url)

# Check the status of the response

if response.status_code == 200:

    # Token is valid; print user information

    user_info = response.json()

    print("Access token is valid.")

    print("User information:", user_info)

else:

    # Invalid token or other error

    print(f"Failed to validate access token. Status Code: {response.status_code}")

    print("Response:", response.json())

# Pause the script to allow the user time to read the response

input("\nPress Return to exit the script...")

Follow GeekSided for guides to using the Facebook API.