Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexandrBasan/961d3658fd5e28110b7a to your computer and use it in GitHub Desktop.
Save AlexandrBasan/961d3658fd5e28110b7a to your computer and use it in GitHub Desktop.
Never expiring Facebook Page Access Token
https://www.rocketmarketinginc.com/blog/get-never-expiring-facebook-page-access-token/
additional 3.3.1
Never expiring Facebook Page Access Token
We’ve been working on upgrading our stats report email that sends our every week detailing key statistics of how that client’s marketing is going. My plan is to have a very robust email detailing key metrics for all the major areas we focus on, including SEO, social media, blogging, email marketing, online ads and more.
To do that I’ve been patching together lots of APIs and database connects to consolidate all the information into this one email. One of those stats I want to display is the client’s Facebook Page views using the insights’ metric page_impressions from the Facebook Graph API.
To do that and allow my weekly email app to run based on a cron job (so it can automatically send out), I needed to get a never expiring access token for this. This took me quite a while to figure out due to the lack of detailed explanation on Facebook’s docs and other places I’d reviewed online. So here is how to get a never expiring Facebook Page Access Token.
Very first thing you need to do is make sure you are an admin/manager of the Facebook page you want a get a never expiring token for. Without that you’re not getting anywhere.
How to get a never expiring Facebook Page Access Token:
1. Created a Facebook APP or used one I already created.
2. Wrote down that Facebook APP’s Id and Application Secret.
3. Got a Short-Lived Access Token from the Facebook Graph API Explorer. Facebook Graph API Explorer can be found here https://developers.facebook.com/tools/explorer
3.1. First selected my app as the Application dropdown.
3.2. Then clicked Get Access Token button.
3.3. When popup opened, I clicked on Extended Permissions and checked boxes for manage_pages, read_insights and read_stream.
3.3.1. When popup opened, click on Request publish_pages (if you have this in your popup)
3.4 Then click Get Access Token on popup window.
4. This gave me an Access Token in the Access Token box. I copied that down. And tested it with the Facebook Access Token Debugger https://developers.facebook.com/tools/debug/accesstoken just to see if it was a token that would expire and it should expire in less than 1 hour. I didn’t need to test this but wanted to just to see what it would say.
5. Then I took that Access Token, the APP’s Id and Application Secret and plugged those into this URL https://graph.facebook.com/oauth/access_token?client_id=APP_ID_HERE&client_secret=APP_SECRET_HERE&grant_type=fb_exchange_token&fb_exchange_token=ACCESS_TOKEN_HERE
6. That URL gave me a Long-Lived Access Token. This expires in 60 days. I tested it to see that using the Facebook Access Token Debugger. Now I tested this Access Token with the Facebook Access Token Debugger and got back that it expires in 2 months (60 days).
7. Now to get the never expiring Page Access Token, I went to Facebook Graph API Explorer and plugged in that Long-Lived Access Token we just created which expires in 2 months. I pasted this into the Access Token box. Then entered into the Get input box “MY_FACEBOOK_USER_ID/accounts” (without quotes). If you don’t know what your Facebook ID is you can get it here http://findmyfacebookid.com/.
8. Then I clicked the Submit button on the Facebook Graph API Explorer page. Although when I did that I got back an error. So I put back into the Get input box what was their originally to see if I could get my id. I put this back in “me?fields=id,name” (no quotes).
9. I got back an id number and my name in the results.
10. I took that id and plug it into the GET input box “MY_FACEBOOK_USER_ID/accounts” (without quotes). Now I got back a list of pages I manage. Along with an Access Token for each page. This Access Token should be a never expiring token.
11. To confirm these were never expiring tokens I tested them with the Facebook Access Token Debugger and got back they never expire.
@DiegoPajares
Copy link

I have a problem on step 10 ... you said that you had a list of pages along with an Access Token for each page....but I only get: {
"data": [
],
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment