douzhendi4559
douzhendi4559
采纳率100%
2013-08-11 19:49

为什么无法获取用户访问令牌(离线)来管理页面?

已采纳

i'm trying to write a script to post to a page while the admin is offline. my application has the manage_pages extended permission of the admin user. here is my code:

require('php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId'  => 'MY_APP_ID', // YOUR APP ID
'secret' => 'MY_SECRET', // YOUR API SECRET
'cookie' => true
));

$user_admin_id = 'MY_ADMIN_ID';
$page_id = 'MY_PAGE_ID';

//get the access token to post to my page via the graph api
$accounts = $facebook->api("/" . $user_admin_id  . "/accounts");

foreach ($accounts['data'] as $account)
{
    if ($account['id'] == $page_id)
    {

    //found the access token, now we can break out of the loop
    $page_access_token = $account['access_token'];
    break;
    }
}

but I always get this message:

"Fatal error: Uncaught OAuthException: A user access token is required to request this resource. thrown in /home/itrade10/public_html/khodiersoftware/php-sdk/src/base_facebook.php on line 1033"

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douyan8413 douyan8413 8年前

    You forgot to authorize the User, that´s how you get a User Access Token:

    https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/

    Don´t forget to add the "manage_pages" permission in the scope Parameter. You will also have to use the Function "setExtendedAccessToken" of the PHP SDK to extend the User Token. After that, you will get a Page Access Token that is valid forever with the /me/accounts endpoint.

    If you used getLoginUrl already, then there´s something wrong with that code, you may want to add it to the question.

    Before getting the accounts (with /me/accounts, not with your id), get the User ID:

    $user = $facebook->getUser();
    

    If you got a valid User Token, your ID will be in the $user Variable.

    点赞 评论 复制链接分享
  • doujingtang6580 doujingtang6580 8年前
  • dongqiangse6623 dongqiangse6623 8年前

    The error message is telling you exactly what the problem is... you need to get a user access token to access your page tokens

    点赞 评论 复制链接分享