5

I want to post to facebook page wall as a page using PHP. I've got access_token by below links.

https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=123456789&redirect_uri=http%3A%2F%2Fmysite.net&scope=publish_stream,manage_pages,offline_access
https://graph.facebook.com/me/accounts?access_token=...

I'm using this simple code:

$appid        = "";
$secret       = "";
$pageid       = "";
$access_token = "";

require_once("facebook-php-sdk/src/facebook.php");

$facebook = new Facebook(array(
  'appId'  => $appid,
  'secret' => $secret
));

try {
    $args = array(
        'access_token' => $access_token,
        'message'      => 'Test', 
        'link'         => 'http://www.test.com',
        'description'  => 'Test'
    );

    $post_id = $facebook->api("/$pageid/feed","post",$args);

} catch (FacebookApiException $e) {
    error_log($e);
}

And that's the error I'm getting:

OAuthException: (#200) Posts where the actor is a page cannot also include a target_id 

But posting /me/feed won't work neither. All solutions I've googled don't work anymore, official documentation didn't help. I got it working when posting as a user (not a page) and with javascript api (required 'share' click action by a user).

Anyone knows the solution for automatic post to a fb page as a page? Spent couple of frustrating days trying to figure it out -_-

Thanks,

A.

akashic
  • 215
  • 4
  • 6
  • PHP code is good, but I got wrong access_token. I got the correct one with different application (simple facebook connect - a wordpress plugin). Don't have time to play with obtaining correct access_token :/ – akashic Feb 24 '12 at 18:13

3 Answers3

7

I've had the same problem. The reason was that I was an admin of more than one page, and I tried to post with the token of another page.

Nikolay Traykov
  • 1,537
  • 17
  • 26
5

Pages cannot post to user's walls (that's why you're getting the OAuthException that says "as a page, you cannot set the target_id"). You'll have to first create an application and get the user's permission (publish_stream) to post to their feed via said app.

typeoneerror
  • 55,990
  • 32
  • 132
  • 223
  • 1
    Thanks for the answer. I'm trying to post to the page wall (thus /$pageid/feed and $access_token). I have application already created (set permissions). I'll take a closer look to stream.publish tomorrow. – akashic Feb 20 '12 at 05:20
  • Ok, found some more time. As written in my first post, my application already has publish_stream rights (2nd line). I managed to post via stream.publish but as REST API is deprecated, I'll try to do it with opengraph. – akashic Feb 24 '12 at 16:41
3

I assume you have some knowledge of app and how posting work.

1.You have to get Page access token and use this on array.

2.you use this

"/$pageid/feed"

try use this

$facebook->api($pageID . '/feed','POST'

Thanks...

Danish Iqbal
  • 1,464
  • 1
  • 13
  • 24