我使用以下PHP代码发布到我的页面墙。没关系,我可以成功发布为管理员,但我想使用页面名称(页面帐户)发布到墙上。如何使用页面名称或页面ID发布。我试图找到很好的资源来解释Facebook API函数,但我没有找到它的好文档。
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access'))}");
exit;
}
else {
echo "i am connected";
}
$attachment = array('message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
?>
答案 0 :(得分:4)
我找到了问题的解决方案。 要作为页面发布或执行任何任务,您需要一个访问令牌。 这是我更新后的最终代码,我想与您分享,因为我发现很多人都难以获得正确的页面访问代码。
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
if($account['id'] == 123456789){ // my page id =123456789
$access_token = $account['access_token'];
echo "<p>Page Access Token: $access_token</p>";
}
}
//posting to the page wall
$attachment = array('message' => 'this is my message',
'access_token' => $access_token,
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
var_dump($status);
?>
答案 1 :(得分:1)
我的Facebook OAuth有点生疏,但我相信这会有所帮助:
http://bugs.developers.facebook.net/show_bug.cgi?id=10005
这是您描述的确切错误,但我无法测试它是否具体纠正了该问题。
答案 2 :(得分:1)
这应该有所帮助:http://developers.facebook.com/docs/reference/api/page/#feed
您似乎需要manage_pages权限。