Intro
I've got a little Facebook fan page I'm posting updates on. To do that I'm using a long lived access token which expires every 60 days with the permissions: 'manage_pages' and 'publish_stream'.
Upon researching I've found no "this is it" solution, so I went creating a new post describing my problem while linking up everything I've found so far as a summary. It grew a little large lol (my apologies).
To get a short lived access token I usually follow these steps as described here.
- Go to https://developers.facebook.com/tools/explorer/ and select your app from the first drop down menu, in the left.
- Click on the button "Get access token", and in the "Select Permissions" window, click in "Extended Permissions" and check manage_pages and publish_stream, and click in "Get Access Token" blue button.
- You may be asked in this step to garant permissions to your app to access to your Facebook account, accept.
- Next, click at the end of the text field next to the "GET" drop down, and replace the numbers for: me/accounts, and click in the blue button next to this text field.
- You'll get the tokens for all your pages, including your app page. Find your page name in the list, will look like this: "name": "Your page name"
- When you located your page, copy the access token for the page (will be really long), that can look like this: "access_token": "XXXXXXXX". Also copy the id of the page: "id": "XXXXX".
Using
https://graph.facebook.com/oauth/access_token?
client_id=[clientid]
&client_secret=[clientsecret]
&grant_type=fb_exchange_token
&fb_exchange_token=[shortlivedaccesstoken]
Either in a browser or by curl request for example, I'm getting my 60 days long lived access token returned. (Source)
As the Facebook API documentation describes, long lived access tokens can not be refreshed.
Of course I can refresh the token using above endpoint and get a new long lived token with the same expiry date returned.
EDIT: To create a post on the fanpage wall (as the page user) I'm doing sth. like this:
$this->fb = new Facebook(array(
'appId' => [app-id],
'secret' => [app-secret]
));
//~Get data from DB here~
//~some error handling in case data fetching failed~
$fbToken = '';
$fbToken = *[facebook access token]*;
$mediaData = array(
'picture' => [picture-url],
'message' => [message],
'link' => [link],
'name' => [name],
'description' => [description],
'access_token' => $fbToken
);
try {
$post_id = $this->fb->api('/' . [fb-page-id] . '/feed', 'post', $mediaData);
} catch(FacebookApiException $e) {
//log file output
}
Problem
You can only get a new long lived access token by using a short lived access token.
@maxim-aniskov describes a way to get a new short lived access token here. Running into the same problem, it works once at the first fetch. The accepted answer involves deletion of the current permissions.
Also, in this post it has been hinted in the comments that even a short lived access token cannot be refreshed without user interaction.
Finally as you can see there are many people running into the same fun here too.
Somewhere I've even read that an existing long lived access token can be refreshed but only if it's about-to-expire. There is no information in which timeframe 'about-to-expire' lies. (5 days ain't)
Ultimately none of them helped me any further.
Question
As a summary of the above mentioned~
How to get a new short lived access token with the previously used permissions (manage_pages and publish_stream) querying an API endpoint?
Is it really only possible by revoking a previously given access?
The website I'm trying to refresh its access token for is about posting real time updates, so I'd like little to no downtime.. Instead of manually generating a new short lived token in the graph api explorer everytime the old token expired or is about-to-expire, I'd rather just press a single button to do that.