Search code examples
facebook-graph-apifacebook-access-token

How do I get a page Access Token that does not expire?


I would like to know if it is possible to have an access token that never expires for post to my page

Now I get the access token with:

https://graph.facebook.com/me/accounts

I have publish_stream and manage_pages permission, but using the Access Token Debugger I see that the token expires in about 1 hour. Is there a way to never expires?


Solution

  • See facebook developers:

    By using a long-lived user access token, querying the [User ID]/accounts endpoint will now provide page access tokens that do not expire for pages that a user manages.

    So, you have to exchange your initial shortlived token for a longlived token with a server side call:

    https://graph.facebook.com/oauth/access_token?
    client_id=APP_ID& client_secret=APP_SECRET& grant_type=fb_exchange_token& fb_exchange_token=EXISTING_ACCESS_TOKEN 
    

    And then query me/accounts with that longlived token. Definitly works for us, i.e. the debugger shows: 'Expires: Never'


    edit - our process

    So, what we do is:

    • first client side authentication with our app where we get a "code" back after the user accepts the requested permissions and connects his account with our app

      https://www.facebook.com/dialog/oauth? client_id=YOUR_APP_ID &redirect_uri=YOUR_REDIRECT_URI &scope=COMMA_SEPARATED_LIST_OF_PERMISSION_NAMES &response_type=code

    • Now in our server application we use server side authentication to exchange code for access token:

      https://graph.facebook.com/oauth/access_token? client_id=YOUR_APP_ID &redirect_uri=YOUR_REDIRECT_URI &client_secret=YOUR_APP_SECRET &code=CODE_GENERATED_BY_FACEBOOK

    • With this access_token we do the server side exchange as described above

    • Now we request me/accounts and the resulting access_token is always valid

    Hope that helps