Python HOW: Making Facebook API calls Using facebook-sdk

Learn how to obtain a permanent Page Access Token for your pages using the official facebook-sdk package. Updated Oct 2020

Image for post
Image for post
Photo captured by the author from developers.facebook.com

You should not depend on these tokens lifetimes remaining the same as Access Tokens can be invalidated or revoked anytime 💀

This might be the case if we make frequent requests using the same Token. Therefore, at the start of each API calling session, we should run the Python code to refresh our Permanent Token

Create an account for developers 🎡

To do this, you only need to be a verified Facebook user. Go to the developers pagecreate an accountMy AppsCreate AppManage Business Integration ▶ Fill in the App details

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
I called my App Python

Get a Short-Lived User Token 🍺

You can only do this manually from the graph API explorer tool page (to read more about access tokens check here). Select the following:

  • User or Page: Get User Access Token Continue Select Page ▶ Done
  • Permissions: Add extra permissions (check the full reference)
  • Generate Access Token when finished
Image for post
Image for post
Graph API Explorer & Access Token info
Image for post
Image for post
Short-Lived User Token info

Exchange with a Long-Lived Token 🍷

This could easily be done by clicking on Extend Access Token in the Access Token Debugger. We can see that a new Long-Lived User Token has been generated for us in the bottom. Click Debug to check its info. We can see that it Expires in about 2 months:

Image for post
Image for post
Image for post
Image for post
Left: Extending the access token. Right: Long-Lived User Token info
  • The app ID and secret. Both can be found in the app dashboard. Navigate to appsPythonAPISettings Basic
Image for post
Image for post
Apps dashboard
user_long_token is a Long-Lived User Token that we can check its info in the Access Token Debugger

Get Permanent Page Access Token 🍹

This could only be done through the facebook-sdk package. Pip install the latest release:

Using a virtual environment is recommended
Check the latest version of the official facebook-sdk release
Image for post
Image for post
Permanent Page Token info

Make a test API request

Let’s make our first API call to get our Page category using the get_object method with the page_id:

Final words

Ideally, we should store all Tokens and the App secret as environment variables (or in a database). However, for simplicity, I’ve used a json file for storage in the full code on GitHub

  • handle_tokens.py handles refreshing tokens

What is next?

In the 👉 article, I go through making different GraphAPI calls to extract useful data and insights from your Page

Written by

I’m an End-to-End data scientist and a Python educator. Most of my articles start after saying “I wish someone has written about this!”, maybe I should?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store