Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

posting to friends wall using facebook javascript sdk

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

like image 258
alexander Avatar asked May 01 '11 01:05

alexander


1 Answers

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 -->
like image 172
John Himmelman Avatar answered Dec 06 '22 22:12

John Himmelman