Using the Facebook Graph API to get public posts and events from your FB Page

Maxwell Struever
3 min readMay 20, 2020

--

Note: I figured this out and it is valid as of May 20, 2020. Facebook updates their APIs often so this may get outdated quickly.

Note #2: Facebook now caps long access tokens at 90 days expiration :(

TL;DR: To get a token that doesn’t expire that has access to public posts of your own Facebook Page, follow the 2 step process here to ‘Get a Page Access Token.’

My website, grabbing Facebook data from our Page

What I was trying to do: I have a website and I want a small section of it to display recent posts and upcoming events for a Facebook Page I manage. I want to be able to do this on my website without having to ask users to login. Since our page posts things publicly, this seems like it should be possible.

I already had a Facebook developer account with an app setup and previously working. Facebook changed some API permissions so your ‘App’ cannot go out and get all public posts from any page or user. To get this kind of access there is a formal application and verification process https://developers.facebook.com/docs/apps/review/feature/#reference-PAGES_ACCESS but I didn’t want to go through this and didn’t feel like I should need to, since these are public posts and I am the manager of the FB page I want to get data from.

Solution: I needed a ‘Page Access Token’

Unfortunately, you cannot generate one of these that does not expire directly from the Graph API Explorer.

To get one that never expires, you want a ‘Long-lived User Access Token.’ To get that, you first get a regular user access token (I generated it easily from the Graph API Explorer)

I selected these permissions but I don’t think that’s even necessary.

With the regular user access token, I generated a ‘Long-lived User Access Token.’ I used Postman

Postman call to get long lived user access token; this basically extends the time of the token generated in Graph Explorer from an hour to 3 months

That will return a response with ‘access_token’ in it. Use that value in the next step:

Use the response access_token from the previous step here in the access_token field

Then, you will get another token response, but it won’t expire!

Once I did this 3 step process, I had a ‘page access token’ that doesn’t expire. I was able to confirm that it didn’t expire on the Token Debugger website.

You’re all set!

What didn’t work: I originally (a few years ago up til last year) was using my App token (it’s shorter and looks like 1234567890|qwertyuiopasdfg ) , and that was able to get the data I needed without expiring.

When that part of the API broke, I was able to generate a token using the Graph explorer with the new permissions required. However, that expired in an hour. I needed a way to have a token that doesn’t expire that I could leave on my web app

Helpful tools:

Facebook Token Debugger: https://developers.facebook.com/tools/debug/accesstoken/ see what a given token has access to, and when it expires

Facebook Graph API Explorer: https://developers.facebook.com/tools/explorer/ generate tokens and test queries

Postman: tool for testing API queries, here is a Postman-export of the two queries I used https://gist.github.com/madmaxlax/e73aac5e7f4cf9c89f3e0688d98f58c3

--

--