作为 PAGE 发布到 PAGE
我正在尝试自动发布(用 PHP)到 Facebook 页面,作为 Facebook 页面。我在获取未过期的令牌然后作为页面发布时遇到问题。看来我可以使用未过期的令牌作为用户发布,但不能作为页面发布。由于文档在示例方面还有很多不足之处,有人可以概述实现这一目标的步骤吗?我创建了一个应用程序,并授予用户管理页面、发布流和离线访问权限。当我尝试发布到页面(没问题)时,它以用户身份发布,而不是页面。我正在努力解决用户未过期访问令牌与页面未过期令牌的概念。
授予权限的代码:
<?
require_once 'facebook.php';
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if(is_null($facebook->getUser()))
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,manage_pages,offline_access'))}");
exit;
}
?>
和发帖的代码:
<?
require_once 'facebook.php';
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$page_id = "PAGE_ID";
$page_access_token = "PAGE_TOKEN";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if( !empty($page_access_token) ) {
$args = array(
'access_token' => $page_access_token,
'message' => "Test post"
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
} else {
}
header("Location: http://example.com/")
?>
I am trying to automate posting (in PHP) to a Facebook Page, as the Facebook Page. I am having issues with getting a non-expiring token, and then posting as the page. It seems I can post as user with the non-expiring token, but not as page. As the documentation leaves a lot to be desired in the way of examples, could someone outline the steps to make this happen? I have created an app, and granted manage_pages, publish_stream and offline_access permissions from user. When I try to post to page (no problem), it posts as user, not page. I am struggling with the concept with un-expiring access tokens for user vs un-expiring tokens for the page.
Code to give permissions:
<?
require_once 'facebook.php';
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if(is_null($facebook->getUser()))
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,manage_pages,offline_access'))}");
exit;
}
?>
and code to make post:
<?
require_once 'facebook.php';
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$page_id = "PAGE_ID";
$page_access_token = "PAGE_TOKEN";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if( !empty($page_access_token) ) {
$args = array(
'access_token' => $page_access_token,
'message' => "Test post"
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
} else {
}
header("Location: http://example.com/")
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您知道 offline_access 已被弃用吗?
Did you know that offline_access is deprecated?
刚刚创建了一个全新的应用程序。确保启用了弃用的离线访问。转到资源管理器,在下拉列表中找到我的新应用程序,授予自己
manage_pages
权限,转到me/accounts
获取其中一个页面访问令牌,然后检查它。糟糕,1 小时过期,所以这不太好。因此,我尝试交换 1 小时的页面令牌,但我收到了来自 Facebook 的错误。我回到用户访问令牌,并将其换成 60 天的令牌。在 linter 中验证这是 60 天。返回到
me/accounts
并获取其中一个页面访问令牌并检查它。惊喜!从那里获得了 60 天的令牌。所以这个故事的寓意是,你不能交换页面令牌,只能交换用户令牌。但使用 60 天的用户令牌,您可以获得 60 天的页面令牌。 :)
just created a brand new app. Ensured the deprecate offline access was enabled. Went to explorer, found my new app in the drop-down, granted myself
manage_pages
, went tome/accounts
grabbed one of the page access tokens, and then linted it.Bummer, 1 hour expiration, so that's not good. So I tried exchanging that 1 hour page token and I got an error from Facebook.I went back to the user access token, and exchanged it for a 60 day one. Verified in the linter that it was a 60 day. Went back to
me/accounts
and grabbed one of the page access tokens and linted it. Surprise! Got a 60 day token from there.So the moral of the story is, you cannot exchange page tokens, only user tokens. But with a 60 day user token you can get a 60 day page token. :)