Skip to content

Instantly share code, notes, and snippets.

@notFloran
Forked from msramalho/instructions.md
Created November 7, 2024 09:18
Show Gist options
  • Save notFloran/5866f9412f567ebce1841fe0c0ca22b2 to your computer and use it in GitHub Desktop.
Save notFloran/5866f9412f567ebce1841fe0c0ca22b2 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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment