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

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

问题描述:

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"

我正在尝试编写一个脚本,以便在管理员离线时发布到页面。 应用程序具有 manage_pages扩展了管理员用户的权限。 我的代码是: p>

  require('php-sdk / src / facebook.php'); 
 $ facebook = new Facebook  (数组(
'appId'=>'MY_APP_ID',//你的APP ID 
'秘密'=>'MY_SECRET',//你的API SECRET 
'cookie'=> true 
))  ; 
 
 $ user_admin_id ='MY_ADMIN_ID'; 
 $ page_id ='MY_PAGE_ID'; 
 
 //通过图表api 
 $ accounts = $ facebook->获取访问令牌以发布到我的页面 api(“/”。$ user_admin_id。“/ accounts”); 
 
foreach($ accounts ['data'] as $ account)
 {
 if($ account ['id'] == $ page_id)  
 {
 
 //找到访问令牌,现在我们可以突破循环
 $ page_access_token = $ account ['access_token']; 
 break; 
} 
} 
  code  >  pre> 
 
 

但我总是收到此消息: p>

“致命错误:未捕获OAuthException:需要用户访问令牌 请求此资源。 在第1033行的 /home/itrade10/public_html/khodiersoftware/php-sdk/src/base_facebook.php 中引用“ p> blockquote> div>

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.

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