Search code examples
phpfacebookfacebook-wall

Posting to facebook page wall via curl


I am trying to post to my facebook page wall using curl but i keep getting the following error

The user hasn't authorized the application to perform this action

How do i generate the correct access token using curl? if you go to this url http://developers.facebook.com/tools/explorer/

I can then put in the following https://graph.facebook.com/337588736279406/feed and this will show my wall feed i can then change this to post and add a field for the message content if i then run this i get the following response.

    {
  "error": {
    "message": "(#200) The user hasn't authorised the application to perform this action", 
    "type": "OAuthException", 
    "code": 200
  }
}

What do i have to do to authorize me as a user??

Can someone help me on this.

    <?php

function postify($arr) {
    $fields_string = '';
    foreach ($arr as $key => $value) {
        $fields_string .= $key . '=' . $value . '&';
    }
    return rtrim($fields_string, '&');
}


$appid = 'app id';
$redirect_url = 'http://stickynote.users36.interdns.co.uk';
$app_secret = 'app secret';
$page_id = '337588736279406';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='.$appid.'&redirect_uri='.$redirect_url.'&client_secret='.$app_secret.'&perms=publish_stream');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

$output = explode("=", $output);

curl_close($ch);

$postdata = array(
    'message' => 'this is a test message',
);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/'.$page_id.'/feed?access_token='.$output[1].'');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, postify($postdata));
$check = curl_exec($ch);

print_r($check);        

?>

Solution

  • To be able to post content you must grant publish_stream permission to your application.

    Requesting permissions can be done using OAuth Dialog.

    BTW, If you just need it for user for testing purposes you may use Explorer Tool by choosing your application, clicking Get Access Token and checking needed permissions.

    Update:
    You will not be able to post on behalf of app, only user or page with appropriate access_token (for page it will require manage_pages and access_token from accounts connection of user). No need to specify perms in access_token retrieval URL. Permissions should be granted before that step.