8
votes

Main objective: How can I get an access token with unlimited validity for a facebook app?

Background information

We have a FB app called MyApp with the following set up:

  • MyApp is authorized to interact with our facebook app
  • MyApp has access rights to manage our pages (manage_pages)
  • MyApp has access to Insights (read_insights)

Our goal is to extract the Insights data automatically, e.g. once every night.

Attempt with oauth generated app token

  1. Get APP_ACCESS_TOKEN belonging to MyAPP

    graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials

    example of retireved token: 328467452729456598|Wn2Gt69Ofg5ySdOGa3TsP2p4R

  2. Use APP_ACCESS_TOKEN to get PAGE_ACCESS_TOKEN for each page graph.facebook.com/me/accounts?access_token=APP_ACCESS_TOKEN

  3. Use PAGE_ACCESS_TOKEN to get the page’s Insights data: graph.facebook.com/YOUR_APP_ID/insights?access_token=PAGE_ACCESS_TOKEN

My problem is that the APP_ACCESS_TOKEN I get from step 1 seems to be missing the user part of the token, resulting in the following error when running step 2:

 "message": "An active access token must be used to query information about the current user.",
 "type": "OAuthException",
 "code": 2500

Attempt with token retrieved from Graph Explorer API token

If I use the APP_ACCESS_TOKEN gained through the Graph API Explorer (https://developers.facebook.com/tools/explorer), I get a token with the user part that is significantly longer. If I use this token in step 2 and 3, I get correct data, but all tokens are only valid for 2 hours, and subsequently I cannot use this for automated retrieval of insights data.

Attempt with exchanging short lived token for long lived token

Following the steps outlined in this guide: https://developers.facebook.com/roadmap/offline-access-removal/#page_access_token, I tried to exchange a short lived token for a longer lived one.

If I use try to exchange the token obtained from the oauth process, I get the error:

  "message": "No user access token specified",
  "type": "OAuthException",
  "code": 1

If I use the token obtained manually from the Graph explorer in the exchange method, I can get the other steps to work as well, but for how long does this new token last? If the token expires after x days or after the some other event, I would still be faced with the issue of obtaining the initial token programatically (as opposed to manually every from the Graph Explorer).

So does anyone know how I can get a long-lived, automatically retrieved token to solve this?

Thanks!

2
You need a user access token to query /me/accounts, not an app access token.CBroe

2 Answers

2
votes

This shell script attempts to help generate access tokens:

https://github.com/dncohen/fb_token

-1
votes

In step 2, you must use the user's access_token to access the /accounts API endpoint. You cannot use the App Access Token here.

What you should do is:

  1. Get the user's access token from Facebook
  2. Exchange the access_token for a long-lived token
  3. Call /accounts to get a long-live page access token
  4. Access page insights using the long-lived token until it expires
  5. Repeat steps 1-4.