Get a Permanent Facebook page Access Token

Benjamin Lienart
4 min readMar 15, 2019

I had to develop for a project, an instagram feed display module. For this project, we decided to use the new GRAPH API for instagram made available to us by Facebook.

To have access to this feed, we need an access token, the User Access Token given by the Graph APi Explorer works, however this one has a very limited life time (about 1 to 2 hours) .

To overcome this problem and be quiet, it will be necessary to generate a permanent Access Token.
I write this article because I did not find in my opinion simple things except a feed StackoverFlow which I was inspired

If you want to save the third and longest steps, I create a script to generate the access tokent for you.
You will find the application here bu you will still have to follow step 1 & 2.

Step 1 — Create a Facebook App (If you already have one, skip this step)

Connect to Facebook Developper (with your facebook account), if you are not already developer, follow the different steps on the site, it’s quite simple.

Once logged in, go to the “My Apps” menu at the top right of the page.
Then click on “Add a new app” then configure your application by adding the category, a website, its name etc …

Generated, this application will give you access to its App_ID and App_Secret which we’ll use in step 3.

You don’t need to manage permissions this application will serve us to generate a Short Lived Access Token.

Step 2 — Generate Short lived User Access Token

To generate our permanent Access Token, we will first need a User Access Token. This one has a relatively short lived (1 to 2 hours).
To get this token, go to the Graph API Explorer of Facebook.

Select the application from which you want to generate the Access Token from the drop-down menu (1)

Then, in the User or Page menu, select “User Token” (2).

Then in the “Permissions”, pull down the “Extended Permissions” menu and select “manage_pages” (3)

Finally click on “Get Access Token”, you will get a User Access Token. Keep it, this one will serve us for the next step.

Step 3 — Generate a Permanent Access Token

To generate our permanent Access Token, we need to have two things :

  • A long live Access Token (60 days)
  • Our User ID

We will use some GET requests to get this informations. You can do it with your browser, postman or even use this application that does the work for you.

If you want to generate it yourself, here are the steps:

1. Generate long live Access Token

To generate a long live Access Token, you will need the following information

  • (1) Your application id (APP_ID)
  • (2) Your secret application (APP_SECRET)
  • Your User Access Token that we generated in step 2 (USER_ACCESS_TOKEN)

Replace {} with the corresponding values:
Note: the current version of the GRAPH API is 3.2, this one can change.

https://graph.facebook.com/v3.2/oauth/access_token?grant_type=fb_exchange_token&client_id={APP_ID}&client_secret={APP_SECRET}&fb_exchange_token={USER_ACCESS_TOKEN}

The answer of the query resseble to this:

{ “Access_token”: “AZERTY123456789”, “token_type”: “….”, “expires_in”: 256212512512}

Keep the “access_token” (LONG_LIVED_ACCESS_TOKEN).

2. Get our User ID

To find our User ID, we will need the LONG_LIVED_ACCESS_TOKEN generated just before.
By the same step, use the following url by replacing the LONG_LIVED_ACCESS_TOKEN with your.

https://graph.facebook.com/v3.2/me?access_token={LONG_LIVED_ACCESS_TOKEN}

Look for the ‘id’ field in the answer, this one represents your User ID (USER_ID).

3. Get your Permanent Access Token.

Finally we get there, to generate this permanent token, make a GET request on:

https://graph.facebook.com/v3.2/{USER_ID}/accounts?access_token={LONG_LIVED_ACCESS_TOKEN}

You will receive your permanent token. Use this one to have the information you need such as access to an instagram feed or something else.

Conclusion

I hope that this article will have interested you, it is my first and I would like to do more in the future, do not hesitate to contact me if you have constructive criticism on this one, I am interested. :)

Here is the app source to generate the token

Have a good day.

--

--

Benjamin Lienart

Développeur Front/End / webdesigner passionné par le web est les nouvelles technologies.