3
votes

I am having problems generating long lived Access Token to update Facebook page from my desktop application.

I created a Facebook App associated with my Facebook user and stored it's app id and secret.

I created a facebook page associated with my Facebook user account.

I can create a short lived (60 minutes) access token for my app.

My desktop application is able to post to my Facebook page using the short lived access token. However, this only lasts 60 seconds and is therefore impractical.

I have been trying to generate a long lived access token using the guidance here:

Request a Page Access Token in C# SDK

However I receive an error:

"The access token does not belong to application XXXXXXXXXXXXXXX"

When I try enter my short lived access token in the debug tool the box, it confirms that the access token is mine BUT it shows a different app id.

Where does this different app id come from? I only have one Facebook app defined.

I think I might be missing a fundamental point here.

Many thanks for any help you can give.

2

2 Answers

7
votes

Go back to the Graph API Explorer where you generated the token.

Make sure that you selected your app from the drop-down at the top where it says "Application[?]"

If you don't it defaults to 'Graph API Explorer' and that will be of no use to you. Once you get the token made for the correct app, try again using this URL: (replace NNNN with the correct data)

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

1
votes

Steps are:

  1. Generate app token with the following Graph API call:

    GET /oauth/access_token? client_id={app-id} &client_secret={app-secret} &grant_type=client_credentials

  2. Exchange received token for a long-lived token (60 days) with the following Graph API call:

    GET /oauth/access_token?
    grant_type=fb_exchange_token&
    client_id={app-id}& client_secret={app-secret}& fb_exchange_token={short-lived-token}

that will give you long-lived token associated with your app. If you want to post on the page AS THE PAGE, then you need to get Page access token - see here for more details: https://developers.facebook.com/docs/facebook-login/access-tokens/#pagetokens

  1. Authenticate the user and request the manage_pages permission
  2. Get the list of pages the user manages from (1): https://graph.facebook.com/me/accounts?access_token=USER_ACCESS_TOKEN
  3. Parse the list and get the token - and use it to post to the feed.

you will do (1) in graph API explorer - and you will get user token. Then insert that token into URL in (2) - and you will see all your pages and corresponding token. Take the one you need and use it in your C# code to upload images.