16

i need to extend my facebook access token, I'm calling this:

https://graph.facebook.com/oauth/access_token? 
client_id={MY PAGE ID}&
client_secret={THE SECRET KEY OF MY APP}&
grant_type=fb_exchange_token&
fb_exchange_token={AN ACCESS TOKEN FOR MY PAGE}

and I'm getting this error:

   "error": {
      "message": "Error validating application. Cannot get application info due to a system error.",
      "type": "OAuthException",
      "code": 101
   }

I've seen a lot of problem with that access_token, but none answer relative to pages, idk why facebook use api that why... but is the way...

Thank you,

Artur Couto
  • 498
  • 1
  • 6
  • 19

2 Answers2

22

To get a long-lived access token you need to follow those steps:

  1. Create an Application
  2. Create a Page (your account need to be "administrator" of the page)
  3. Associate the application to the Page (the same way you do it when you want to add a Page Tab to a Page)

    http://facebook.com/add.php?api_key=*YOUR_APP_ID*&pages=1&page=*YOUR_PAGE_ID*
    
  4. Get a short-lived access token with the permission "manage_pages" associated to your Application.

    https://graph.facebook.com/oauth/authorize?client_id=__APP_ID__&scope=manage_pages&redirect_uri=http://www.facebook.com/connect/login_success.html
    then
    https://graph.facebook.com/oauth/access_token?client_id=__APP_ID__&redirect_uri=http://www.facebook.com/connect/login_success.html&client_secret=__APP_SECRET__&code=__CODE_FROM_PREVIOUS_REQUEST__
    
  5. Using the Graph API Explorer with the request /me/accounts you can see the access tokens for each Pages that you are administrator. The problem is that those access token are short-lived.

  6. Convert your short-lived access token to a long-lived (extending access token):

    https://graph.facebook.com/oauth/access_token?client_id=_APP_ID_&client_secret=_APP_SECRET_&grant_type=fb_exchange_token&fb_exchange_token=_ACCESS_TOKEN_ON_STEP_4_
    
  7. You can now test your new access token with the Access Token Debugger.

FR6
  • 3,157
  • 3
  • 24
  • 28
  • question: My app is not 'tab app' how do i associate it ? – siniradam Mar 11 '13 at 12:35
  • 1
    @siniradam Use this URL: http://facebook.com/add.php?api_key=*YOUR_APP_ID*&pages=1&page=*YOUR_PAGE_ID* – FR6 Mar 11 '13 at 15:08
  • Thanks for that. BTW Yesterday I discovered if I send a request to /me/accounts with long-term token, list returns with long-term tokens. I've tested with this: https://developers.facebook.com/tools/debug/access_token?q= – siniradam Mar 12 '13 at 08:14
  • 2
    Hey @FR6 just wanted to say thanks for the step by step, I sunk quite a bit of time into this the last two days and this guide worked perfectly. – Andrew Bartel May 16 '13 at 18:37
  • How do you associate your application to a page? – chaostheory Jul 30 '13 at 19:24
  • Note that `6.` is not needed if the token from `4.` is long lived. If it is, the tokens returned from `/me/accounts` will **never** expire (https://developers.facebook.com/docs/facebook-login/access-tokens/#extendingpagetokens). – Prinzhorn Sep 14 '13 at 12:03
2

Scenario 5: Page Access Tokens

When a user grants an app the manage_pages permission, the app is able to obtain page access tokens for pages that the user administers by querying the [User ID]/accounts Graph API endpoint. With the migration enabled, when using a short-lived user access token to query this endpoint, the page access tokens obtained are short-lived as well.

Exchange the short-lived user access token for a long-lived access token using the endpoint and steps explained earlier. By using a long-lived user access token, querying the [User ID]/accounts endpoint will now provide page access tokens that do not expire for pages that a user manages. This will also apply when querying with a non-expiring user access token obtained through the deprecated offline_access permission.

https://developers.facebook.com/roadmap/offline-access-removal/

Gajus
  • 69,002
  • 70
  • 275
  • 438