posting to friends wall using facebook javascript sdk

alexander picture alexander · May 1, 2011 · Viewed 20.6k times · Source

How can I post to a friends wall using the facebook javascript SDK?

Answer

John Himmelman picture John Himmelman · Aug 15, 2011

In addition to @ifaour's answer on FB.api...

  • Set oauth and status parameters in FB.init
  • Request extended permission, publish_stream, via FB.login

Other resources:

using facebook stream publish

Complete example...

<script>

window.fbAsyncInit = function()
{
    FB.init({
        appId  : 'APP_ID',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true , // parse XFBML
        oauth : true // Enable oauth authentication
    });

    FB.login(function(response)
    {
        if (response.authResponse)
        {
            alert('Logged in!');

            // Post message to friend's wall

            var opts = {
                message : 'Post message',
                name : '',
                link : 'http://www....',
                description : 'Description here',
                picture : 'http://domain.com/pic.jpg'
            };

            FB.api('/FRIEND_ID/feed', 'post', opts, function(response)
            {
                if (!response || response.error)
                {
                    alert('Posting error occured');
                }
                else
                {
                    alert('Success - Post ID: ' + response.id);
                }
            });
        }
        else
        {
            alert('Not logged in');
        }
    }, { scope : 'publish_stream' });
};

</script>

<!-- FACEBOOK -->        
<div id="fb-root"></div>
<script>
(function() {
  var e = document.createElement('script');
  // replacing with an older version until FB fixes the cancel-login bug
  e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
  //e.src = 'scripts/all.js';
  e.async = true;
  document.getElementById('fb-root').appendChild(e);
  }());
</script>
<!-- END-OF-FACEBOOK -->