How to create a non-expiring Facebook page token as described in the Removal of offline_access permission doc

妖精的绣舞 提交于 2019-12-11 05:41:07

问题


I am trying to create a non-expiring page token that I can use on my site to make posts on my Facebook page. According to scenario 5 in this document I should be able to do this:

https://developers.facebook.com/roadmap/offline-access-removal/

My problem is that when I generate the page tokens, they expire in 1 hour.

What I am doing is using this code to display my user access token when I login. I think this is the same token provided by graph api explorer, but I am unsure.

require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'app_id',
  'secret' => 'app_secret',
));   

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }   
}     

if ($user) {
  $logoutUrl = $facebook->getLogoutUrl( array(

                'scope'         => 'read_stream,publish_stream,publish_actions,manage_pages,email,user_checkins',
                ));
} else {
  $loginUrl = $facebook->getLoginUrl();
}
echo $loginUrl;

$me = $facebook->api('/me');

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>

  </body>
</html>

I then use that token in this graph api command:

https://graph.facebook.com/me/accounts?access_token=<access_token>

This gives me a list of all my pages and apps and their respective page/app tokens.

I then take the page token I want to use and check with the Facebook Access Token Debugger and it shows it has an expiration of 1 hour. So I use the following command to try and exchange it for a 60 day token as described in the first URL i posted:

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

The command just gives me the error: "message": "An unknown error has occurred.","type": "OAuthException", "code": 1.

I then thought maybe because my user access token is not long lived, it wont generate a long lived page token. So I used my user token with the same command to extend it and it just returns the same token back with the expiration not extended.

Can anyone offer some insight as to what I am doing wrong and how I can create this non-expiring page token?


回答1:


Read the instructions again carefully - based on your description you're:

  • Getting a short user token
  • Getting a short page token from /me/accounts
  • Attempting to extend the short page token

What you should be doing is:

  • Getting a short user token (if client side auth)
  • Exchanging that for a long user token (or you'll already have a long user token if you used the server side oauth flow)
  • Using the long user token to retrieve the (long) page access token


来源:https://stackoverflow.com/questions/12770720/how-to-create-a-non-expiring-facebook-page-token-as-described-in-the-removal-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!