我正在使用facebook的php SDK,而我正试图在由登录用户管理/管理的页面上发布。我已经授予了publish_stream和manage_pages权限。我希望帖子应该看起来像是由页面而不是管理员或登录用户。我尝试了几个帮助,但没有人工作。这是我现有代码的一部分:
require './php-fb-sdk/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook( array('appId' => 'xxxx', 'secret' => 'zzzz'));
// Get User ID
$user = $facebook -> getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook -> api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$params = array("scope" => array("manage_pages", "publish_stream"));
// Login or logout url will be needed depending on current user state.
if ($user) {
// Fetch the viewer's basic information
$basic = $facebook -> api('/me');
$permissions = $facebook -> api("/me/permissions");
if (array_key_exists('manage_pages', $permissions['data'][0]) && array_key_exists('publish_stream', $permissions['data'][0])) {
$admin_pages = $facebook -> api(array('method' => 'fql.query', 'query' => "SELECT page_id, type from page_admin WHERE uid=me() and type!='APPLICATION'"));
if (count($admin_pages) > 0) {
$post_info = $facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));
echo '<hr>The post info is: ' . print_r($post_info, true) . '<hr>';
} else {
echo '<hr> You are not admin of any fb fan page<hr>';
}
//print_r($admin_pages);
} else {
// We don't have the permission
// Alert the user or ask for the permission!
header("Location: " . $facebook -> getLoginUrl(array("scope" => "manage_pages,publish_stream")));
}
$logoutUrl = $facebook -> getLogoutUrl();
} else {
//$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook -> getLoginUrl($params);
$statusUrl = $loginUrl;
}
使用上面的代码我可以在页面上发布,但它看起来像是由用户制作的。
但是,如果我使用facebook JS SDK,那么我看到帖子看起来就像是由页面制作的。
var data=
{
caption: 'My Caption',
message: 'My Message'
}
FB.api('/' + pageId + '/feed', 'POST', data, onPostToWallCompleted);
}
非常感谢任何帮助或建议。
答案 0 :(得分:1)
要代表网页在网页上发帖,您需要使用页面访问令牌。您当前的电话:
$facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));
正在使用默认(用户)访问令牌。
要获取页面访问令牌,请在发布post:
之前进行此调用\GET /{page-id}?fields=access_token
这会在结果中为您提供一个页面访问令牌,然后只需使用它来进行发布Feed调用,就像这样 -
$facebook -> api(
'/' . $admin_pages[0]["page_id"] . '/feed',
'post',
array(
"caption" => "From web",
"message" => "This is from my web at: " . time(),
"access_token" => '{page_access_token}'
)
);
(如果需要,您还可以获得页面永不过期的令牌,请点击此处查看:What are the Steps to getting a Long Lasting Token For Posting To a Facebook Fan Page from a Server)