SDK:在页面上以页面形式发布

发布于 2024-12-25 13:21:53 字数 2063 浏览 4 评论 0原文

我在将页面上的帖子添加为页面时遇到问题。 我使用以下代码:

$app_id = "xxx";
$app_secret = "xxx";
$my_url = base_url().'admin/facebook/';

if(isset($_GET["code"])) {
    $code = $_GET["code"];
} else {
    $code = '';
}
if(empty($code)) {
 $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
 $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
   . $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=manage_pages,publish_stream,offline_access&state="
   . $_SESSION['state'];

 echo("<script> top.location.href='" . $dialog_url . "'</script>");
}

if($_GET['state'] == $_GET['state']) {

 $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

 $response = @file_get_contents($token_url);
 $params = null;
 parse_str($response, $params);

 $graph_url = "https://graph.facebook.com/me?access_token=".$params['access_token'];

 $user = json_decode(file_get_contents($graph_url));
 echo("Hello " . $user->name);

    $pageID = '212154388861617';


    // post to wall (feed is wall post, just update to whatever you want to publish to)
    try {
        //153406078098666
        $publishStream = $this->fb_ignited->api("212154388861617/feed", 'post', array(
            'message' => "Test message",
            'access_token'    => $params['access_token']
            )
        );
    } catch (FacebookApiException $e) {
        die($e);
    }
}
else {
 echo("The state does not match. You may be a victim of CSRF.");
}

该帖子已正确添加到我的页面上,但该帖子被添加为我的用户帐户而不是我的页面帐户。 我如何以页面形式发布? 或者这不可能吗?

解决方案:我添加了这段代码

$result = $this->fb_ignited->api("/me/accounts",  array('access_token'    => $params['access_token']));
         foreach($result["data"] as $page) {
            if($page["id"] == $pageID) {
                $page_access_token = $page["access_token"];
                break;
            }
         }

I have a problem with adding a post on a page as a page.
I use the following code:

$app_id = "xxx";
$app_secret = "xxx";
$my_url = base_url().'admin/facebook/';

if(isset($_GET["code"])) {
    $code = $_GET["code"];
} else {
    $code = '';
}
if(empty($code)) {
 $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
 $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
   . $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=manage_pages,publish_stream,offline_access&state="
   . $_SESSION['state'];

 echo("<script> top.location.href='" . $dialog_url . "'</script>");
}

if($_GET['state'] == $_GET['state']) {

 $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

 $response = @file_get_contents($token_url);
 $params = null;
 parse_str($response, $params);

 $graph_url = "https://graph.facebook.com/me?access_token=".$params['access_token'];

 $user = json_decode(file_get_contents($graph_url));
 echo("Hello " . $user->name);

    $pageID = '212154388861617';


    // post to wall (feed is wall post, just update to whatever you want to publish to)
    try {
        //153406078098666
        $publishStream = $this->fb_ignited->api("212154388861617/feed", 'post', array(
            'message' => "Test message",
            'access_token'    => $params['access_token']
            )
        );
    } catch (FacebookApiException $e) {
        die($e);
    }
}
else {
 echo("The state does not match. You may be a victim of CSRF.");
}

The post is correctly being added on my page but the post is being added as my user-account and not as my page-account.
How can i post as a page?
Or isn't this possible?

Solution: I added this piece of code

$result = $this->fb_ignited->api("/me/accounts",  array('access_token'    => $params['access_token']));
         foreach($result["data"] as $page) {
            if($page["id"] == $pageID) {
                $page_access_token = $page["access_token"];
                break;
            }
         }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2025-01-01 13:21:53

您正在使用用户的 access_token,而您需要使用页面 access_token

这可以通过 Graph API 从 accounts 连接获取>user 一旦用户向您的应用程序授予 manage_pages 权限

You're using user's access_token while you need to use page access_token.

This can be obtained via Graph API from accounts connection of user once user granted manage_pages permission to your application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文