简体   繁体   English

Facebook Post链接作为页面问题

[英]Facebook Post Link as Page Issue

I am writing a simple Facebook page tab app that will allow me to post messages (status updates, links, photos and videos) to multiple pages. 我正在编写一个简单的Facebook页面标签应用程序,它将使我可以将消息(状态更新,链接,照片和视频)发布到多个页面。

I am having an issue with the Facebook API (using the PHP SDK) trying to post various things to a page's timeline. 我在尝试将各种内容发布到页面的时间轴上的Facebook API(使用PHP SDK)遇到问题。

When I post a status update, it successfully posts under the name of the page. 当我发布状态更新时,它会在页面名称下成功发布。

    $pages = $this->facebook->api('me/accounts', 'GET');
    $pages = json_decode(json_encode($pages), FALSE);

    foreach ($pages->data as $page) {
        if (in_array($page->id, $_POST['pages'])) {

            $data = array(
                'access_token' => $page->access_token,
                'message' => Arr::get('status', $_POST)
            );

            $queryString = http_build_query($data);

            $this->facebook->api("$page->id/feed?".$queryString, 'POST');
        }
    }

As soon as I add any more parameters (ie attempt to post a link) the post is made under my admin account (ie Chris Hayes posted a link to 'Page X'). 一旦我添加了更多参数(即尝试发布链接),该帖子就会在我的管理员帐户下发布(即Chris Hayes发布了指向“ Page X”的链接)。

    $pages = $this->facebook->api('me/accounts', 'GET');
    $pages = json_decode(json_encode($pages), FALSE);

    foreach ($pages->data as $page) {
        if (in_array($page->id, $_POST['pages'])) {

            $data = array(
                'access_token' => $page->access_token,
                'link' => Arr::get('link', $_POST),
                'message' => Arr::get('status', $_POST)
            );

            $queryString = http_build_query($data);

            $this->facebook->api("$page->id/feed?".$queryString, 'POST');
        }
    }

I have no idea what's going on here. 我不知道这是怎么回事。 Literally the only thing that has changed is adding the 'link' parameter. 从字面上看,唯一改变的是添加“ link”参数。 If anyone can help me I'd greatly appreciate it! 如果有人可以帮助我,我将不胜感激!

Edit The permissions I've acquired are: email, user_about_me, user_likes, user_birthday, manage_pages, publish_stream 编辑我获得的权限是:电子邮件,user_about_me,user_likes,user_birthday,manage_pages,publish_stream

Regards, Chris 克里斯,问候

I found the answer in another post. 我在另一篇文章中找到了答案。

    $pages = $this->facebook->api('me/accounts', 'GET');
    $pages = json_decode(json_encode($pages), FALSE);

    foreach ($pages->data as $page) {
        if (in_array($page->id, $_POST['pages'])) {

            $this->facebook->setAccessToken($page->access_token);

            $data = array(
                'access_token' => $page->access_token,
                'name' => 'Facebook API: Posting As A Page',
                'link' => 'https://www.webniraj.com/2012/08/09/facebook-api-posting-as-a-page/',
                'caption' => 'The Facebook API lets you post to Pages you own automatically - either as real-time updates or in the case that you want to schedule posts.',
                'message' => 'Check out my new blog post!'
            );

            $queryString = http_build_query($data);

            $this->facebook->api("$page->id/feed?".$queryString, 'POST');
        }
    }

The key was to set the page access token via the Facebook SDK. 关键是通过Facebook SDK设置页面访问令牌。 This may be because I use the library elsewhere to set the access token as the user's; 这可能是因为我在其他地方使用该库将访问令牌设置为用户的令牌。 I guess the library must override the token sent in as a parameter. 我猜该库必须覆盖作为参数发送的令牌。

$this->facebook->setAccessToken($page->access_token);

Reference: Posting to a Facebook Page as the Page (not a person) 参考: 以页面的形式发布到Facebook页面(不是个人)


EDIT: Another issue I just discovered is that I was building the query string myself and appending it to the API endpoint, like so: 编辑:我刚刚发现的另一个问题是我自己构建查询字符串并将其附加到API端点,如下所示:

$this->facebook->api("$page->id/feed?".$queryString, 'POST');

What you are supposed to do is send the $data array in as the third parameter of the API call: 您应该做的是发送$ data数组作为API调用的第三个参数:

$this->facebook->api("$page->id/feed", 'POST', $data);

The PHP SDK looks for an 'access_token' parameter in the $data, and because I wasn't sending in that array it wasn't finding it (and thus defaulted to using my user access token I had set previously). PHP SDK在$ data中寻找一个'access_token'参数,因为我没有在该数组中发送数据,所以找不到它(因此默认使用我之前设置的用户访问令牌)。

After sending the data in as the third parameter of the API call I was able to remove the line: 在将数据作为API调用的第三个参数发送之后,我能够删除以下行:

$this->facebook->setAccessToken($page->access_token);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM