douzhendi4559 2013-08-11 19:49 采纳率: 100%
浏览 65
已采纳

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

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 2013-08-11 20:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记