Skip to content

Instantly share code, notes, and snippets.

@msramalho
Last active May 15, 2024 10:16
Show Gist options
  • 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}
@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!

@janez33
Copy link

janez33 commented May 9, 2024

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

Basically this video explains THE BEST and EASIEST WAY how can you get LONG LIVED token to access read_insights, ads_read and ads_management permissions :)
https://youtu.be/22u1cMLxylU

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