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条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系