0

Nothing gets posted to the wall, execution gets out of try after $result = $facebook->api('/me/feed/','post',$attachment); statement, any idea whats broken.

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

// Get User ID


$user = $facebook->getUser();
if ($user) {
  try {
    // Get the user profile data you have permission to view
    $user_profile = $facebook->api('/me');
    $uid = $facebook->getUser();


      $url = $facebook->getLoginUrl(array(
    'canvas' => 1,
    'fbconnect' => 0,
    'req_perms' => 'email,publish_stream,status_update,user_birthday,user_location,user_work_history'));


    $attachment = array
 (
 'access_token'=>$facebook->getAccessToken(),
 'message' => 'I had a question: should I write a PHP facebook app that actually worked?',
 'name' => 'I Asked Bert',
 'caption' => 'Bert replied:',
 'link' => 'http://apps.facebook.com/askbert/',
 'description' => 'NO',
 'picture' => 'http://www.facebookanswers.co.uk/img/misc/question.jpg'
 );
 echo "Test 1"; 

$result = $facebook->api('/me/feed/','post',$attachment);

    echo "Test 2"; 
    $_SESSION['userID'] = $uid;


  } catch (FacebookApiException $e) {
    $user = null;
  }
} else {
  die('Somethign Strange just happened <script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}

Test 1 is printed but not Test 2.

6
  • The key to your error is: The user hasn't authorized the application to perform this action. Looks you have not enabled the facebook app for the user or not set the permissions for the user to post to the wall.
    – Treffynnon
    Aug 27, 2011 at 22:59
  • @Treffynnon wht shd i do to get it enabled. if by tht u mean changing the migration setting of the app i have enabled all of them now . and another point to note is error is not in my default page where the above code is its in base_facebook.php on line 970
    – PUG
    Aug 27, 2011 at 23:11
  • possible duplicate of post to facebook wall - PHP
    – bkaid
    Aug 28, 2011 at 1:29
  • plz note i edited this question, apologies for confusion
    – PUG
    Aug 28, 2011 at 2:15
  • This exception is coming : 1OAuthException: (#200) The user hasn't authorized the application to perform this action
    – PUG
    Aug 28, 2011 at 2:57

3 Answers 3

5

You said you were looking for updated documentation, did you check Facebook PHP-SDK FAQ?

Specifically,

  • How to authorize and have any of the following permissions?
  • How to post on a wall?

After you create an Application instance get your $user first

$user = $facebook->getUser();

From here, following the instructions from "How to authorize and have any of the following permissions?" using the scope

$par = array();
$par['scope'] = "publish_stream";

Check the user state to see which login/logout method is required passing the publish_stream permission

if ($user) {
        $logoutUrl = $facebook->getLogoutUrl();
} else {
        $loginUrl = $facebook->getLoginUrl($par);
}

Then place the attachment as explained in "How to post on a wall?"

if ($user) {
$attachment = array('message' => 'this is my message',
    'name' => 'This is my demo Facebook application!',
    'caption' => "Caption of the Post",
    'link' => 'http://mylink.com/ ',
    'description' => 'this is a description',
    'picture' => 'http://mysite.com/pic.gif ',
    'actions' => array(array('name' => 'Get Search',
    'link' => 'http://www.google.com/ '))
    );

    try {
    // Proceed knowing you have a user who is logged in and authenticated
    $result = $facebook->api('/me/feed/','post',$attachment);
    } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
    }

}

As explained in the example app, put in a try/catch block to see what data is available depending on whether the user is logged in or not when making API calls.

A call such as

$cocacola = $facebook->api('/cocacola');

Will always work since it is publicly available.

2
  • This exception is coming : 1OAuthException: (#200) The user hasn't authorized the application to perform this action
    – PUG
    Aug 28, 2011 at 2:59
  • @jam Go to your applications and see what permissions the app was granted.
    – phwd
    Aug 28, 2011 at 3:08
1

Here are a couple of notes:

  1. You are using the new PHP-SDK so don't use req_perms use scope instead
  2. Put your /me/feed post call inside the try
  3. You don't need to call getUser() twice, the user id is already in the $user
  4. The user id will be already in the session, with key that looks like: fb_XXXXXXX_user_id where XXXXXXX is your app id
  5. session already started..
2
  • says here facebookanswers.co.uk/?p=206 for php 5 use req_perms, did try the scope though nothing got posted. Thanks for the suggestion though & sorry about the ambiguity in question.
    – PUG
    Aug 28, 2011 at 2:14
  • no if you read the article correctly, the new PHP-SDK now use the scope paramter
    – ifaour
    Aug 28, 2011 at 14:31
0

below code surely works: even if you dont have permission it will try to get them

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
$user_profile = $facebook->api('/me');


if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
                // Permission is granted!
                // Do the related task
                //$post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!'));
                  $post_id = $facebook->api('/me/feed', 'post', $attachment);

            } else {
                // We don't have the permission
                // Alert the user or ask for the permission!
                echo "Click Below to Enter!";
                header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );
            }

*Warning

as of sept 5 2011 this is working, but i saw on fb documentation they are changing method to poste on users wall and are discouraging use of publish stream. but its working for now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.