简体   繁体   中英

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.

I am having an issue with the Facebook API (using the PHP SDK) trying to post various things to a page's timeline.

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').

    $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. 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

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. 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)


EDIT: Another issue I just discovered is that I was building the query string myself and appending it to the API endpoint, like so:

$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:

$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).

After sending the data in as the third parameter of the API call I was able to remove the line:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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