Long-lasting FB access-token for server to pull FB page info
Asked Answered
D

6

120

I'm aware that there are many questions about Facebook access-tokens and the grief they cause, but despite much experimentation and reading many frustratingly vague blog articles (FB and otherwise), I'm still struggling to get a clear answer to my needs. Let me succinctly break down my process so far:

  • I am creating a site that, server-side, needs to pull the posts/statuses from a single Facebook Page
  • I am an admin of that Facebook Page
  • I have created a Facebook App
  • Using the Facebook Graph API Explorer, I've generated a short-lived key, connected to my app and my account, that grants permission to my account to view the access-tokens for my pages
  • I've converted my short-lived key to a long-lived key (60 days) ala scenario 4 from this

And here's where I am stuck. My 60 day key works fine for my server to pull the info needed from the page, but as far I can tell, there's no way to programmatically extend that 60 day key. I also do not know of a way to generate a new short-lived key without manually going to the Facebook Graph API Explorer and creating one.

Since it is my server making the requests to the Facebook API and not a user-based system (where I could easily request that a user authorize the Facebook app again), this creates a very clunky system. Since Facebook deprecated offline_access, is there really no permanent way to have my server pull info from my own page? Will I really have to create a new key by hand and manually update my server with it every 60 days?

Or is there something I'm missing?

Update:

The step-by-step guide that was previously found here has been migrated down into its own answer.

Dallasdalli answered 28/8, 2012 at 22:31 Comment(11)
This is awesome. I'm in the same situation and was looking for some info on this topic. Is it safe that this token doesn't break any FB platform policies? Just asking to be sure.Fleet
@asrijaal Facebook's own documentation (take a look at Scenario 5) says that these page access tokens do not expire. I'd say it's safe to assume that they comply with Facebook's policies.Dallasdalli
"I thought it'd be good to leave a clear step-by-step process" - heaven forbid :) Thanks @DallasdalliWattage
You have a typo with your page access token url. Should be a ? not a & after accounts. Spent a while trying to figure out why this wasn't working. :PNosy
@Nosy Indeed - it is fixed now. Thanks!Dallasdalli
You don't need to be an admin, the read-only role, analyst, can work as well. You just need to give the manage_pages right. I suppose the page token will only allow read-only access then, but it's all I needed.Honoria
One warning: this token does not expire, unless user changes password, then it will become invalid.Hypocoristic
than you so much for this. also- what the hell facebookCoset
I took up an experiment for reading data from Facebook through Pentaho-Kettle, and found that certain things (read API) have changed since these tutorials have been written... Here's my collection of the process... Thanks to the original poster a great deal... tech.sraghav.in/2014/02/…Bev
@redhotvengeance, since your “update” is the answer that actually works, would you mind moving it to an answer below so that it can be voted on and accepted?Twitt
@JuanA.Navarro After thinking it over (and testing the steps again to make sure Facebook hasn't changed things up), I agree. It better follows SO conventions for those steps to be in their own answer. I've migrated them.Dallasdalli
D
195

These are the steps that were previously in the question - they have been migrated to this answer.

Having found that it is possible to generate a Facebook Page Access Token that does not expire (with help from @Igy), here is a clear, step-by-step quide for all those looking to the same:

  1. Make sure you are the admin of the FB page you wish to pull info from
  2. Create a FB App (should be with the same user account that is the page admin)
  3. Head over to the Facebook Graph API Explorer
  4. On the top right, select the FB App you created from the "Application" drop down list
  5. Click "Get Access Token"
  6. Make sure you add the manage_pages permission
  7. Convert this short-lived access token into a long-lived one by making this Graph API call: https://graph.facebook.com/oauth/access_token?client_id=<your FB App ID >&client_secret=<your FB App secret>&grant_type=fb_exchange_token&fb_exchange_token=<your short-lived access token>
  8. Grab the new long-lived access token returned back
  9. Make a Graph API call to see your accounts using the new long-lived access token: https://graph.facebook.com/me/accounts?access_token=<your long-lived access token>
  10. Grab the access_token for the page you'll be pulling info from
  11. Lint the token to see that it is set to Expires: Never!

That should do it. You should now have a Facebook Page Access Token that doesn't expire, unless:

  • You change your Facebook account password
  • You lose admin access for the target page
  • You delete or de-authorize your Facebook App

Any of these will cause the access token to become invalid.

If you are getting (#100) Tried accessing nonexisting field (accounts) on node type (Page), go to the Access Token Debugger, copy the value of User ID, and use it to replace the "me" part of the URL in step 9.

Dallasdalli answered 21/2, 2014 at 7:11 Comment(23)
How and where do I do step 6?Froe
@StefanMüller When you click "Get Access Token" on the Graph API Explorer page, a "Select Permissions" dialogue pops up. The manage_pages option is under the Extended Permissions tab.Dallasdalli
Great! This works like a charm, and this published as the PAGE, not as a user.Wesle
Thanks it help lot to get never expire accesstoken.Comparator
Just a warning. I already had a "long-lived access token" and I was trying to use it with this code but it did not work. Make sure you follow every stepTaima
I believe this is out dated now. and you only get an access token which expires in about two months.Butane
@QaiserWali I just tested the process, and it still works fine. You sure you did every step? The token in step 7 has an expiration, but the token you get from step 9 should be set to never expire.Dallasdalli
thks for the answer ..If I exchange the short lived access token for long lived token will my access token have all the permissions my short lived token had?Heterophyte
it says Error validating client secret. How should I solve thisHeterophyte
News at 11! Following all the steps helps in achieving said result. Why would people follow only some of the steps and then say it doesn't work??Knoll
Note that for step 7 you cannot use the Graph API Explorer. It will bug out on this (my guess is this is due to the response not being JSON). Simply, you can just go to the URL using your browser. Wish I had found your answer sooner, I just wrote my own, more verbose version a week ago.Suspensor
Facebook messed this up so it doesn't work anymore. :-( After step 6 above (clicking the "Get Access Token" button) a red warning appears in the following dialog, stating "Submit for Login Review - Some of the permissions below have not been approved for use by Facebook" and also a padlock with the text "This does not let the app post to Facebook" appears at the same time below. So a new step should be inserted between step 6 and 7 above, reading "Do a lot of tedious sh*t with your app, then submit it for review, then pray and wait." :-( Anyone know a way around this for a pure test app?Pulido
I have done all of that now and it works except that the long-lived token id expires after 2 months and is not infinite. Also the Acces Token Debugger has a link at the bottom of the form to extend a short-lived access token to a long-lived access token.Fagen
works fine 11/15/2015 the only tricky part is 5/6 you have to click the box that opens the dialog with optionsMerrile
this works for me (I think you have to make sure you do the "request publish pages" part in the access token dropdown in Graph now, instead of permissions tick in step 6).When I take my page's token ID from list of all my pages and run it through Linter I get Expires: NeverKenwood
Hint for #5/6, you want the "Application" dropdown that is right below the "My Apps" dropdown.Adrianneadriano
I assume that for step 5, instead of "Get Access Token" it should now be "Get User Access Token"? Either way, having got my never-expiring token using this method, I'm getting this error when trying to post to my page via the API: (#200) The user hasn't authorized the application to perform this action.Trompe
I solved my problem, with this. Since API v2.3, to publish posts to your page as your page, you need to include the publish_pages permission when getting the initial access token. (Docs)Trompe
Why isn't this on facebook developers docs? Thank you so much dudeEpenthesis
@KenthJohnIsrael this is now included in facebook docs: developers.facebook.com/docs/facebook-login/access-tokens/… Extending Page Access Tokens [...] To get a longer-lived page access token, exchange the User access token for a long-lived one, as above, and then request the Page access token. The resulting page access token will not have any expiry time.Fortress
For those of my brothers, who are getting token for 2 month, first please understand the concept. 1. PageAccess Token -- > 2 Month Page Access Token --> Error in Step 9 2. UseAccess Token --> 2 Month User Acess Token -- > Receives List of tokens Now copy Page Access Token which you receive in response.. This is still working as of Nov 2017Procopius
My understanding is that this thread has been inactive for about two years, so I'm checking with users here if this solution remains valid up to the present day? I have indeed tried the steps enumerated above and it worked; i.e. I got a token that allows my app to programmatically read/publish on my FB page, and when I look at the Token Debugger, I do get Expires: Never. What concerns me though is the field right below (in the debugger output) that says Data Access Expires: in-3-months. I'm also wondering, maybe there is now (in 2019) a new way of implementing this (legit) use case?Enormous
As I'm mentioning in my previous comment, it appears that access to the data will expire after around 3 months, so the solution is no longer viable projects that don't want to use manual updates. So I did a little more research and found a better way (supported by Facebook) of doing it in 2019. See my answer below: https://mcmap.net/q/120503/-long-lasting-fb-access-token-for-server-to-pull-fb-page-infoEnormous
R
17

This is covered in the Offline Access deprecation document

Use the 60-day token for the page admin to retrieve a Page Access Token (via /PAGE_ID?fields=access_token or /me/accounts) - the Page access token will not have an expiry time

Roz answered 28/8, 2012 at 22:49 Comment(3)
Sigh. Indeed, this seems to be the ticket. I could have sworn that I tried every possible combination of these steps - apparently I missed the one that actually works. What an absolutely convoluted set of required actions. Thanks for much for your help!Dallasdalli
So many pieces all over the internet, this is one that actually works.Larson
Is there any way exists to get this 60 days page_access_token by code in javascript? Like the way of getting user_access_token > FB.getAuthResponse()['accessToken']; Thanks!Cringe
E
11

An approach that works in 2019

I was recently trying to achieve something similar (to the use case described in this thread), but I wanted to make sure to respect Facebook's current policies, so I did a little research and here I'm sharing what I found.

My use case

So, as I said already, my use case is very similar to the one described here; that is:

  • I'm doing some work for a school district.
  • They are using a software tool to manage pretty much everything that relates to school transportation.
  • That tool allows them to send email notifications (to subscribers) when they publish bus delay alerts and school closure alerts.
  • A lot of people in the community follow the organization on their Facebook page, and that's the only place they look for those alerts.
  • So an employee of the organization has to manually publish each notification on the Facebook page (in addition to creating it in the transportation software). Moreover, those notifications eventually expire (or are simply deleted before they expire), so the employee has to go back later on to delete them manually as well.
  • It's a waist of time, so what we are trying to do here is to develop as simple system that periodically polls the software tool's database for new (and expired) notifications and update them (i.e. add and remove) on the Facebook page.

This is, in my view, a legitimate use case, but I wasn't sure how to implement it in a way that's in line with Facebook's policies.

The accepted answer

I followed the steps of the accepted answer and it worked, except that things appear to have changed: now, even though the generated page token does not expire, access to data does expire after around 60 days. You will see that as well if you follow the procedure and inspect the page token in the FB Token Debugger Tool.

Besides, the fact that the generated page tokens are tied to the user account is also unfortunate, because if the user updates his/her password, then the page token also gets invalidated.

How to do it in 2019

After several hours of research, I stumbled upon the following Facebook documentation article: Business Login for Direct Businesses.

It turns out that it is now possible, following the steps described in the above article, to generate a page token that is not associated to any particular Facebook user account and which will not expire (unless the FB App gets deleted or the underlying application token gets deleted, you know...)

So here are the steps and the most important parts:

  • You need a Business Manager account.
    • Verification will be required and a digital contract will have to be signed.
  • You need to add the target Facebook page to that account.
  • You need to create a Facebook App, and transfer that app to the same Business Manager account as well.
  • The app will have to go through Facebook's review process, because the following permissions will be needed: manage_pages and publish_pages.
    • Important note For the posts made using the generate page token to be visible to users other than the application administrators, that app will need to have been published and approved.
    • You may still experiment with the concept without submitting for review, but the posts won't be publicly visible.
  • In the Business Manager account (only after your app and page have been added to the account), you need to create what's called a System User, and give that user admin role (or permissions) to the target Facebook page.
    • A system user is owned by the Business Manager account, and isn't tied to a specific user. My current understanding is that one major use case for a system user is programmatic access to Facebook's Graph API (just what we need).
  • Then, for that system user, you need to generate a access token (which will be never-expiring). You will be prompted to select for which app. You will then select your target app.
  • You will then need to use the generated app token to generate a page token, which will also be never-expiring. The procedure is described in this article as:
GET /<PAGE_ID>?fields=access_token&access_token=<SYSTEM_USER_ACCESS_TOKEN>
  • That's it.

That token will never expire, and it won't be tied to a particular Facebook user, so it's exactly what we need!

The last part is to make sure that your Facebook app gets approved by Facebook. It's in fact the most important part, because the whole procedure is worthless if people don't see our posts.

I wanted to know for sure that I could rely on the above procedure to build something for my client without Facebook rejecting it in the end, so, beforehand (i.e. before starting to work on my client's project), I went through the whole process of creating a page, an app, a Business Manager account, etc. I verified my business. I submitted my app for review. In my request, I was very specific about my use case and emphasized that the app was for "self-use" (i.e. that the organization is developing an app for itself, not for other Facebook users). I got approved without less than 24 hours.

A few other notes about the app review process:

  • I had to select a platform for the app, so I selected website.
  • I had to indicate why the app needed the two permissions and how it was going to use them.
  • I had to indicate why the reviewer would not be able to sign into my app and try it (i.e. because the app will be used by a worker process).
  • For the mandatory screencasts, I simply presented manual operations in the terminal using the curl utility (to generate the page token and make posts to the Facebook page). I also showed how I was using Business Manager to link the system user to the page and generate a token, and so on.
  • Again, I was very specific about my use case, and I think that that helped.

I hope this information will be useful to people with similar use cases.

Enormous answered 15/11, 2019 at 13:39 Comment(1)
Wow. Thanks for the detailed write-up. I can't believe how insane the process is just to gain programmatic access to the API. 😖Duple
F
2

Many thanks to @redhotvengeance for step-by-step guide.

After some time, now there is clearly described in Facebook documentation:

https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension

Extending Page Access Tokens

Apps can retrieve a page access token from Page admin users when they authenticate with the manage_pages permission. If the user access token used to retrieve this page access token is short-lived, the page access token will also be short-lived.

To get a longer-lived page access token, exchange the User access token for a long-lived one, as above, and then request the Page access token. The resulting page access token will not have any expiry time.

Fortress answered 25/8, 2017 at 19:34 Comment(0)
K
0

You can also copy and past from the app dashboard on facebook. The steps:

  1. Go to https://developers.facebook.com

  2. Select your app in the top right corner of the page (pic of what it looks like)

  3. Click on Messenger from the options on the left (it will go to setting automatically) (pic of what it looks like)
  4. Go to the "Token Generation" section in the page. Select what page you want to generate the token for. (pic of what that section looks like)
  5. The copy and past your page token where ever you need it.

Keep in mind that while in theory your token won't expire, that it is directly tied to what ever facebook account your logged into. So say you change your password or you remove the permissions from between your account and your app then your token won't be valid any more.

Kurtis answered 27/10, 2017 at 16:40 Comment(0)
A
0

I have figured out the current, easy way:

  1. Get User Access Token: First, you need a user access token with the necessary permissions to manage the page. You can get this user token via the Facebook Graph API Explorer

  2. Exchange for a Long-Lived User Access Token: User access tokens are short-lived (an hour or so). You can exchange it for a long-lived token:

    curl -i -X GET "https://graph.facebook.com/v18.0/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={short-lived-token}"
    
  3. Get Page Access Token: Once you have a user access token, you can get a page access token.

    curl -i -X GET "https://graph.facebook.com/v18.0/{page-id}?fields=access_token&access_token={user-access-token}"
    
Aristophanes answered 11/1 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.