Skip to content

Instantly share code, notes, and snippets.

@msramalho
Last active April 14, 2024 18:21
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save msramalho/4fc4bbc2f7ca58e0f6dc4d6de6215dc0 to your computer and use it in GitHub Desktop.
Save msramalho/4fc4bbc2f7ca58e0f6dc4d6de6215dc0 to your computer and use it in GitHub Desktop.
How to get a Facebook long term (non-expiring) access token for a Page

0 - Open the token validator

Go to https://developers.facebook.com/tools/debug/accesstoken/ and use this page to test your tokens at each step, namely looking at their expiry date.

1 - Get a short-lived token

Go to developers.facebook.com and generate a User token with the permissions required.

To post to pages you need pages_show_list, pages_read_engagement, pages_manage_posts, public_profile.

This is {your-access-token}, the short live access token (1h).

2 - Get a long-term token from that (2 months)

curl -i -X GET "https://graph.facebook.com/{graph-api-version}/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={your-access-token}"

# {graph-api-version} >= v10.0
# {app-id} = name of the app in https://developers.facebook.com/tools/explorer
# {app-secret} = secret from the app page > settings > basic > App Secret
# {your-access-token} = the User token from an account with admin permission for the facebook page

# returns your {long-lived-user-access-token} as access_token (2 months)

3 - Get your user id

https://graph.facebook.com/v2.10/me?access_token={access_token}

# access_token is your long term access token

# return your {user-id} 

4 - Get a non-expiring token

Take the token from the 2nd step and pass it as {long-lived-user-access-token}:

curl -i -X GET "https://graph.facebook.com/{graph-api-version}/{user-id}/accounts?access_token={long-lived-user-access-token}"

# {graph-api-version} >= v10.0
# {user-id} = id of the user (not of the app) from step 3
# {long-lived-user-access-token} = token from step 2

# returns a final data array with {access_token}
@GarryOne
Copy link

In my case, it's returning an empty data:

{"data":[]}

@HariHend1973
Copy link

got the same result

@msramalho
Copy link
Author

This has not been tested in a while so may have changed on facebook's side, please share if you have an up-to-date guide.

@HariHend1973
Copy link

sure, will do.

@d4vidsha
Copy link

I just tested this and it still works for me.

@mayesa
Copy link

mayesa commented Mar 13, 2023

Same here. Getting {"data":[]}

@jonathanbova73
Copy link

In step 3 I am getting a "Malformed access token" when using the token returned in step 2

@duivesteyn
Copy link

duivesteyn commented Jun 29, 2023

code in step 3, Line 1 should read
curl -i -X GET "https://graph.facebook.com/v2.10/me?access_token={access_token}"

@joelcorey
Copy link

Looks like this is working and/or has been fixed. Thanks for this.

@trustaff
Copy link

I know this post has been marked as resolved. I am getting the same empty data as users mentioned earlier. could there be an issue with Facebook. if anyone has any updated answers please share. Thanks.
I am getting {"data":[]}%

@orangevinz
Copy link

orangevinz commented Jan 31, 2024

I just tried it and it works.
For me, running the query curl -i -X GET "https://graph.facebook.com/{graph-api-version}/{user-id}/accounts?access_token={long-lived-user-access-token}" returns a page token.
Interestingly, after this query, the previously used long-lived token is now marked as having an "expiration: never" in the facebook debug tool

@janez33
Copy link

janez33 commented Feb 5, 2024

is this also possible to get a long lived token to access facebook insights data?

@AmadejKrepek
Copy link

It worked as charm for me today.

@janez33
Copy link

janez33 commented Feb 12, 2024

@msramalho Hi, is this also possible to get a long lived token to access facebook insights data?

@msramalho
Copy link
Author

msramalho commented Feb 12, 2024

I've not used it to look into those @janez33 but welcome any insight you may find!

@sharma39vishal
Copy link

Thanks @msramalho for sharing this. I have been searching for it for last 3 days.

@encodiaweb
Copy link

encodiaweb commented Mar 2, 2024

I am using v19.0 and I am getting {"data":[]} executing the last step.

I am trying to access:

pages_show_list
pages_read_engagement

@AndriyRubanskui
Copy link

I am using v19.0 and I am getting {"data":[]} executing the last step.

I am trying to access:

pages_show_list
pages_read_engagement

Add business_management permission

@rat1Na
Copy link

rat1Na commented Mar 7, 2024

Perhaps you should try it.

I managed to obtain a long-term token using the Facebook Lite + App Cloner APK
and until now, it's still working well...

Check out the tutorial video here => https://youtu.be/zcQ_YW8IktU

@howardfernandez
Copy link

Thanks for sharing this @msramalho ...very helpful!

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