简体   繁体   中英

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

?>

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.

You need to get a page access token to do that.

In the documentation it says:

Page access_token

An access_token used to manage a page. This is used when you want to perform an operation acting as a Page. This access token is retrieved by issuing an HTTP GET to /USER_ID/accounts or to /PAGE_ID?fields=access_token with the manage_pages permission. Getting /USER_ID/accounts will return a list of Pages (including app profile pages) to which the user has administrative access in addition to an access_token for each Page. Alternatively, you can get a page access token for a single, specific, page by issuing an HTTP GET to /PAGE_ID?fields=access_token with the manage_pages permission (you must ask for the access_token field specifically via the fields= parameter). See the documentation for the Page object for more information

After you get the access token you can then make those api calls.

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