banesto
banesto

Reputation: 1672

Get facebook page access token to automatically publish to facebook page wall

There is a need to publish stories from website to specified facebook page via PHP. Offline access permission is deprecated, so I basically need to get user access token so that I can get page access token (to post as page), but these tokens expire and extending them requires asking user directly to extend permissions in pop-up (correct me if I'm wrong).

So maybe there is a way to login in background so to speak, so that if user access token is expired, i log user in and get new access token? Or maybe there is a way to get never-ending user access token and obtain never-ending page acces token through that?

Don't understand why it's so hard to do, especially if i own website and facebook page to which I'm willing to publish to.

Upvotes: 3

Views: 5788

Answers (2)

toraman
toraman

Reputation: 603

I just found out how to do this and searched stackoverflow for the question so that I can answer it :)

First, it's not the user you have to ask for extended access token, it's you. You need to extend your own user's access token. Your user's access token can be found at https://developers.facebook.com/tools/accesstoken/ under the name 'user token'.

Next step is to extend this token's life by making this request:

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

Where {app-id} and {app-secret} are the info at the dashboard of you application in developers.facebook.com and the {short-lived-token} is the 'user token' you just found.

You don't have to make this call via php SDK you can just use your browser like requesting the page graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={short-lived-token} (Note: It returns the value in the form of access_token={access-token}&expires={seconds}. So don't copy the whole thing.)

Now, when you get your page access token using this extended user token, it will be immortal. Make this API call using the new token (Note: Before making the call, you have to grant the permissions 'manage_pages' for making this call and 'publish_actions' for posting to the page later. The easiest way to do this is to hit the "get access token" button at https://developers.facebook.com/tools/explorer/ the permissions you need to give are under the 'extended permissions' tab.):

GET /me/accounts

You will get the list of your pages and some of their information along with an access token for each of them.

Those access tokens do not expire. You can check at https://developers.facebook.com/tools/debug/

Upvotes: 6

cpilko
cpilko

Reputation: 11852

You need to create a connector app for your page, then get a permanent access token for your app. You can see the details of this process on the Authenticating as an App page on the Facebook documentation.

Upvotes: 0

Related Questions