3

I have a facebook page and i want the feeds of page. I am using graph api to get the access token but i have no idea how to convert that short-lived access token to long-lived 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=EXISTING_ACCESS_TOKEN

If above link is used to get the long live access token then from where i can get App_Secret
Help Me if i am going on wrong approach.

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
VG108
  • 122
  • 2
  • 2
  • 10

2 Answers2

13

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)

  4. Get a short-lived access token with the permission "manage_pages" associated to your Application.

  5. https://graph.facebook.com/oauth/authorize?client_id=APP_ID&scope=manage_pages&redirect_uri=http://www.facebook.com/connect/login_success.html

  6. 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

  7. 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.

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

  9. 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_

    You can now test your new access token with the Access Token Debugger.

Pragati Singh
  • 233
  • 2
  • 6
  • Pragati - can u give detail step how to associate application to page – VG108 Jan 08 '13 at 09:53
  • 1
    here __APP_ID__ is App ID and __APP_SECRET__ is App Secret of your application when you pass these value correctly and follow the above steps yours page will automatically associate with yours app. – Pragati Singh Jan 10 '13 at 11:18
  • Whats a mess, so If i want to get a feed of a page's lasts posts, I need an app token? – Ernesto May 04 '16 at 21:22
11
  1. First of all, learn the basic concepts and the different kinds of the access tokens from here

  2. To get the extended User token (validity: 2months) use the code you have mentioned in the question.

    You can get the app secret from the Apps page.

  3. To get a never expiring token for your fan page. Follow the simple steps:

    • Using the user token you obtained through step 2, get the list of pages/apps-

      $facebook->api("/USER_ID/accounts"); 
      
    • Get the never expiring access token for any page-

      $facebook->api("/PAGE_ID?fields=access_token");
      

(You can use Facebook's Debug Tool to check the validity of the token.)

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • sahil- on the graph api explorer tool, i have put the "/PAGE_ID?fields=access_token" but in response i am not getting anything – VG108 Jan 08 '13 at 08:29
  • then i guess you haven't followed each step properly. Check out the access token you are using – Sahil Mittal Jan 08 '13 at 09:02
  • sahil- i have created a facebook page ,and i got the page_id and on graph api explorer i have just pasted it and run the query . This is waht i have done ,do let me know if i have done anything wrong – VG108 Jan 08 '13 at 09:14
  • first run - `/me/accounts`. Then click on the id you are looking for. Then append `?fields=access_token` in the field. This will give you temp page token. Then replace the access token in above field with the extended token of user you obtained in step 2 , and run the same query- you will get the immortal page token. If still not works , start chat. – Sahil Mittal Jan 08 '13 at 09:31
  • sahil - with /me/accounts i am not getting id of page i am just getting id of app ,how can we chat -new to stackoverflow – VG108 Jan 08 '13 at 09:47
  • How could i help you then? :-/ – Sahil Mittal Jan 08 '13 at 10:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22374/discussion-between-vg108-and-sahil) – VG108 Jan 08 '13 at 10:02
  • @VG108 I've followed your steps and everything seems to work, but when the user logs out the token is invalidated. Is there a way to have a page access token that works even if the user is logged out? Am I doing something wrong or there's no way to that? – AldoB May 31 '13 at 14:39