6

I'm trying to post on user's Facebook wall from my app. The user granted the permissions to the app to post on his wall, and I have userid in db. I need to send the post automatically, without the user logging in again.

My code is:

try{
    require_once(dirname(__FILE__) . '\lib\facebook-php\src\facebook.php' );
}
catch(Exception $o){
        print_r($o);
}
$config = array(
    'appId' => '123456',
    'secret' => '454544',
    'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);

$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_id = '123456';

if($user_id) {

    // We have a user ID, so probably a logged in user.
    // If not, we'll get an exception, which we handle below.
    try {

        $ret_obj = $facebook->api('/me/feed', 'POST',
            array(
                'access_token' => $facebook->getAccessToken(),
                'link' => 'www.example.com',
                'message' => 'Posting with the PHP SDK!'
            ));
        echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

        // Give the user a logout link
        echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
    } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        var_dump($e);
        $login_url = $facebook->getLoginUrl( array(
            'scope' => 'publish_stream'
        ));
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
    }
} else {

    // No user, so print a link for the user to login
    // To post to a user's wall, we need publish_stream permission
    // We'll use the current URL as the redirect_uri, so we don't
    // need to specify it here.
    $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
    echo 'Please <a href="' . $login_url . '">login.</a>';

}

This throws An active access token must be used to query information about the current user. error and asks for login. After I click login, the post is sucesfully posted.

How can I get the access token with userid without the user logging in?

1
  • Version note: the above code has been replaced with SDK V4.
    – Raptor
    Commented May 26, 2015 at 11:19

2 Answers 2

8

This happens when the active access token is not available while calling the API end point /me

There are two solutions to get rig of this

  • use the userid instead of /me

  • set the access-token prior sending the api call

For the first you can make the call as

ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
            array(
                'link' => 'www.example.com',
                'message' => 'Posting with the PHP SDK!'
            ));

You dont need to send 'access_token' => $facebook->getAccessToken(),

The Second option is to do as

$access_token =  $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
ret_obj = $facebook->api('/me/feed', 'POST',
                array(
                    'link' => 'www.example.com',
                    'message' => 'Posting with the PHP SDK!'
                ));

NOTE: publish_stream scope must be send while doing the login

Deprecation Notice :publish_stream was replaced by publish_actions, check the facebook doc for permission

0
0

you have to get permission . Check it out here

https://developers.facebook.com/docs/reference/login/

3
  • I have Facebook login implemented on the site. The user already granted the permissions to the app to post on his wall. Do I need any other permission? Commented Feb 19, 2014 at 20:03
  • in your login URL you have to pass the permissions in SCOPE check this code of my login URL. $login_url = $facebook->getLoginUrl(array('scope' => 'photo_upload,read_mailbox')); Commented Feb 20, 2014 at 5:08
  • may be you will need to access publish_actions permissions in your app. so it will be like $login_url = $facebook->getLoginUrl(array('scope' => 'public_actions')); Commented Feb 20, 2014 at 5:10

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.