12

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

alexander
  • 125
  • 2
  • 2
  • 5

2 Answers2

10

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 -->
Community
  • 1
  • 1
John Himmelman
  • 21,504
  • 22
  • 65
  • 80
4

You need to use the FB.api method, something like:

var body = 'Reading Connect JS documentation';
FB.api('/FRIEND_ID/feed', 'post', { message: body }, function(response) {
  if (!response || response.error) {
    alert('Error occured');
  } else {
    alert('Post ID: ' + response.id);
  }
});
sakibmoon
  • 2,026
  • 3
  • 22
  • 32
ifaour
  • 38,035
  • 12
  • 72
  • 79
  • hey I tried this and I got a POST http://myipaddress/Discover/FacebookItemRecommend 404 (Not Found), why is this – aherlambang May 01 '11 at 14:48
  • @aherlambang: Well, without seeing your code I won't be able to answer you. How about asking a new question with your code and the error you are getting – ifaour May 01 '11 at 14:55
  • File: https://connect.facebook.net/en_US/all.js Line No: 85 Error Detail: Error: Domain contained invalid characters. – Shahid Karimi Mar 18 '13 at 11:10