0

Possible Duplicate:
Facebook Graph Api - Posting to Fan Page as an Admin

I've written the code below:

$pageId = 'xxx';
$appId = 'xxx';
$secret = 'xxx';
$facebook = new Facebook(array(
    'appId' => $appId,
    'secret' => $secret,
    'cookie' => false
));

$access_token = $facebook->getAccessToken();

$facebook->api("/{$pageId}/feed", "post", array(
    'message' => "First message",
    'name' => "Name for first",
    'access_token' => $access_token
));

But getting the following exception: (#200) The user hasn't authorized the application to perform this action

Community
  • 1
  • 1
VuesomeDev
  • 4,095
  • 2
  • 34
  • 44

2 Answers2

0

Have you checked if the user is logged in yet? Simple, but I don't see it in your code.

You could first check if $facebook->getUser() is null, if null redirect to $facebook->getLoginUrl(array('req_perms' => 'user_status'); and make sure to add other permissions you might need.

This question is very similar, read through some of the answers - there should be one that works for you.

Community
  • 1
  • 1
Brian Hansen
  • 206
  • 2
  • 6
0

You will need *publish_stream* permission in your code. It can be:

$my_url = "http://apps.facebook.com/siegiusarena";

// Get User ID $user = $facebook->getUser();

if ($user) {

    $message=$application_name." game on facebook. Check it out: http://apps.facebook.com/siegiusarena";
$link = "http://apps.facebook.com/siegiusarena";
$attachment = array('message'=>$message, 'link'=>$link, 'picture'=>'http://www.developas.com/fb/siegiusarena/siegiusarena_256x256.jpg');
$facebook->api("me/feed", 'post', $attachment);

}else{ $code = $_REQUEST["code"];

    if(empty($code)) {
                     $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
                     $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
   . $appid . "&redirect_uri=" . urlencode($my_url) . "&state="
   . $_SESSION['state'] . "&scope=email,publish_stream";

                    echo("<script> top.location.href='" . $dialog_url . "'</script>");

} }