If you’re trying to write your own script to download your Facebook Page’s Insights information, or if you’re using Extrata, you need a Facebook API key. The problem with the keys is that they are short-lived by default, and it’s not obvious how to get one that doesn’t expire.
Facebook makes you jump through hoops to get such a key. Out of all the services I’ve done API work with, Facebook is the only one where you can’t get a permanent key in less than a minute.
I searched for multiple solutions, but some of them were outdated, or didn’t explain the steps that clearly. I’m going to try to do that for you here.
As you read the instructions, click on any of the screen shots below to see the image in full size. Note that I ran through this process several times to verify the process, so some of the timestamps on the screenshots may not be in sync.
At any point during the process, you may be re-authenticated by Facebook. Do what you need to do to complete the authentication, and you will be allowed to continue to the next step.
I’ve marked a couple of “checkpoints” where you need to copy a piece of text that will be used later in the instructions.
Create an App
Go To: https://developers.facebook.com/
If you haven’t already logged in, log in using your Facebook account.
After you’ve authenticated, click on My Apps, and in the resulting menu, choose “Add New App”
You’ll get a popup. Provide a Display Name for your application. Use something self-explanatory like “My Facebook Insights Data”
Click on “Create App ID”.
You may be challenged with a Captcha or other security notice. Do what you need to do and submit.
Your app will be created. You might be asked to provide additional information, but you can skip that step. At this point, you want to create a Page Access Token.
Create a Page Access Token
To create a Page Access Token, you need to go to the Graph API Explorer, which is available in the Tools menu in the header. Alternatively, you can simply go to this link.
Important Note: For these instructions, I’m using the new Graph API Explorer. At the time of writing (January 2019), this was in “Beta”, but it may be live by the time you read this post. If the Beta is still active, you will see a banner at the top of the Graph API Explorer Page with a “Try it out now!” link. Click on that link before proceeding with these instructions.
In any case, Facebook may change the layout and structure of the site without notice, so these instructions may not be valid when you read them.
At the Graph API Explorer page, click on the dropdown beneat “Facebook App” and select the name of the App you created in the previous step.
Click on the User or Page dropdown. Depending on your situation, you may get put on one of two paths.
- A menu option called “Get Page Access Token”
- A subheader “Page Access Tokens” with a list of pages you have access to.
Item 2 will generally only appear if you’ve already gotten a page access token in the past for the app.
If you got Item 1 above, click on the “Get Page Access Token” option.
You may be authenticated and asked for additional information.
If you get the popup above, click “Continue as [your user name]”
If you get the popup above, click on “Choose what you allow”. The popup below will appear.
Select the Page(s) you’d like your App to have access to. This list is based on your personal account ID. Facebook already knows what pages you administer and will only let you choose from one of them. If you don’t see the page you want, it means you need to get administrative access for that page.
If you got Item 2 above, click on the Facebook page for which you’d like to access via Facebook’s API in that drop down menu.
Enable Insights Permission
Go to the “Add a Permission” dropdown and click on Other. This will make more permissions visible in the drop down. Scroll down and click on “read_insights”.
This will add “read_insights” to the Permissions box above the drop down. It might appear in green text as shown below.
Click on “Get Access Token”. Note you may be re-authenticated at this step. Accept and/or submit whatever you need to continue.
If the “read_insights” permission was green, it will probably turn black after this step.
Go back up to “User or Page” and re-select the name of the Facebook Page you’d like to generate a token for. This will repopulate the Access Token field at the bottom of the rightmost pane.
Click on the blue info icon (the letter “i” in a blue circle). You should get an information balloon for the current short-lived token.
In the information balloon, make sure there is a row with the label “Page”. To the right of a label is a number, a colon, and the name of the page. For example: 12345678 : My Page Name
Write down or copy that number, because we will need it later.
In the row with the label Scopes, make sure that “read_insights” is in the list. If it is not, you need to retry all the steps in this section (“Enable Insights Permission”)
Generate Expiring Tokens
Click on the Open in Access Token Tool. This will open up a new tab or window in your browser. Note that every time you click on this button or a “Debug” button, a new tab will appear. You might want to keep track of which new tab was opened by which action you took.
The new tab will be an Access Token Debugger window. Below the heading is a text box containing a long random string, which is your short-lived Access token. It is the long blurred out box in the image below.
On this page, you need to verify a few things.
- That the Type is Page
- That the Page ID matches the Facebook Page you want insights for
- That the Scopes contains the “read_insights” permission
After making sure that this token is correct, click on the Extend Access Token button. The button will disappear and a new box will appear at the bottom of the page.
You should now have a token that expires in two months.
Note that sometimes you will get lucky and get your never expiring token at this step. If that’s the case, copy the token in the text box at the top of the page and save it in a safe place. You can ignore the rest of the instructions.
Click on the “Debug” button (with a gray background) beside the new Token. As mentioned earlier, this will open up a new window or tab in your browser.
Verify the information for this new token. Make sure that it is a Page Token and that it expires in two months and has the “read_insights” permission.
Copy the token at the top of the page (to the left of the blue “Debug” button).
Generate Non-Expiring Token
Go back to the browser tab that had the Graph API Explorer.
Paste that token you copied (that expires in 2 months) in the “Access Token” field at the bottom of the left pane. You’ll notice that the contents of the pane refreshed. The “User or Page” field might say “User Token” instead of for the page you want, but you can ignore that for now.
Click on information icon (letter “i” in a light blue circle) to get the information for the key you just pasted. Verify that there is a Page row and that the Scopes row contains “read_insights”. Also, the Expiration Time should be two months in the future.
At the top of Graph API explorer, there’s a text box that should contain “me?fields=id,name”. Replace this text with the Page ID you copied earlier from Checkpoint 1. If you forgot to do that, it’s the numeric portion in the Page row from the Access Token Info popup from the last step. At the end of the Page ID you pasted, add the text “?fields=access_token”
For example, if your Page ID is 12345678, then the contents of the box should be 12345678?fields=access_token
Click on the blue Submit button at the top of the page.
You should receive a JSON object with two attributes: access_token and id. Copy the contents of the access token (but don’t include the two quotation marks). You might need this later.
Go back to the information icon (letter “i” in a light blue circle) in the Access Token field and click on it. The information for the token should remain unchanged. Click on the “Open in Access Token Tool” button.
This will open yet another browser tab with the Access Token Debugger.
In this window, the verify the information for your token. Make sure the Type is Page. You should notice that in the Expires row, the value should be “never”. You now have a non-expiring Facebook Insights Access Token. Copy your token, which is in the text box above the Access Token Info table (the one with the blue Debug button on its right) and save it somewhere safe.
If this step didn’t work, I find that pasting the access token from the JSON object you copied at Checkpoint 2 into the text field in the Access Token Debugger and clicking on the Debug button will also produce a never expiring token.
Keep in mind that at any time Facebook can throw a wrench into the process and make these instructions invalid. I’ll try to make edits to this post periodically to make sure it’s up-to-date.