Joe Huang
Joe Huang

Reputation: 6570

How to get/use User access token for Facebook Open Graph (as a cron job)?

I use following code with Facebook PHP-SDK to access a page feed:

require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxxxxx',
  'secret' => 'yyyyy',
));

$feed = $facebook->api("/page_id/posts");

It works fine. The code is used in a cron job in the web server to retrieve page feeds automatically and periodically, therefore there is no user interaction to log in or log out.

If I print the access code by $facebook->getAccessToken(), I can see the access code is actually a "App access token". (Please refer to https://developers.facebook.com/docs/facebook-login/access-tokens/ , there are four kinds of access tokens)

What if I want to use a "User access token" instead in such a cron job (no physical user to login), how to do it? Is it possible?

Thanks.

Upvotes: 2

Views: 4670

Answers (1)

Sahil Mittal
Sahil Mittal

Reputation: 20753

Extending the user's access token

You can exchange the user's access token with the long-lived access token (2-months) validity. Go though the link to get the long-lived token. But then you have to refresh the token after 60 days by the same login process.

Extending tokens

Extending the page's access token

As an alternative, you can have a never expiring token for your fan page, that will solve your problem too I guess. To get this token-

Make the following call using the long-lived user's access token -

    $facebook->api("/PAGE_ID?fields=access_token");

(You can use Facebook's Debug Tool to check the validity of the token)

Upvotes: 1

Related Questions