Print this page

How to auto publish post on Facebook Fan Page as admin using Facebook PHP SDK V4 Graph API v2.2 (2) - Get Permanent Page Access Token

Graph API Explorer

There are various types of access token for facebook app. The best article that explains them is How to handle access tokens with Facebook SDK v4.0 by Sammy Kaye. To post article / item / product from our own website to facebook fan page, we need to use Page Access Token.

The initial token can be obtained from Facebook Graph API tool. However, that is just a short-lived access token, which will expire in an hour or two after generation. We need to made a Graph API call to get a long-lived access token, which has two months expiry date. Lastly, get a permanent access token, which will never expire and can be used in our daily script.

We can test our script using short-lived access token, but then you have to regenerate another token after expiration. So we should directly get permanent access token before writing our script.

1) First, go to Facebook Developers Tools, click on Graph API Explorer.

 Facebook Tools Debug

 

2) We see the first page of Graph API Explorer. At "Graph API Explorer" button, select the newly created app and click "Get Access Token".

Graph API Explorer 

 

3) We need to select relevant permissions for our purpose of our app. Auto posting to facebook page does not require user data. So we skip this menu and go to "Extended Permissions".

User Data Permissions 

 

4) Select "manage_pages" and "publish_actions" at Extended Permissions. Click "Get Access Token".

Extended Permissions 

 

5) There will be a few pop up windows, click "Okay" for each of them.

Extended Permissions

Extended Permissions

Extended Permissions 

 

6) Finally, we get the short-lived Page Access Token at the tool panel. Copy this access token.

short live Page Access Token 

 

7) To check this access token, we can use facebook access token linter. Paste the token into the debug box.

Facebook Access Token Linter 

 

8) As we can see, this token will expire in an hour.

Lint short live access token 

 

9) Now, we want to exchange with long-lived access token for our app. Construct this Graph API Call URL and post at your browser URL box.

https://graph.facebook.com/oauth/access_token?client_id=< Your App ID >&client_secret=< Your App secret >&grant_type=fb_exchange_token&fb_exchange_token=< Short-lived token > 

 

10) The Graph API call returned this:

access_token=< Long-lived token >&expires=5184000

Note: When you lint this token to debug box, it showed no expiry date for this token. However, the Graph API call gave us  "expires=5184000" -- 60 days, which is still an open question for me

 

11) To exchange this long-lived token to permanent token, paste this to URL box of your browser. Make sure you still login facebook that owns the app.

https://graph.facebook.com/me/accounts?access_token=< Long-lived token > 

 

12) Now find the facebook page name and that's the permanent Page Access Token.

Permanent Page Access Token 

 

13) Check it again just to make sure. Paste the token into Lint debug box. It says Never expire.

Page Access Token Never Expire 

Now we can work on our script with above App ID, App Secret and permanent Page Access Token.

 

 

Last modified on Tuesday, 29 December 2020 07:03
Rate this item
(2 votes)