使用facebook javascript sdk发布到朋友墙

时间:2011-05-01 01:48:40

标签: facebook facebook-javascript-sdk

如何使用facebook javascript SDK发布到朋友的墙上?

3 个答案:

答案 0 :(得分:10)

除了@ ifaour在FB.api上的答案......

  • FB.init
  • 中设置oauth和状态参数
  • 通过FB.login
  • 申请扩展权限,publish_stream

其他资源:

using facebook stream publish

完整示例......

<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 -->

答案 1 :(得分:4)

您需要使用FB.api方法,例如:

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);
  }
});

答案 2 :(得分:-1)

<script type="text/javascript">
var friendIds=new Array();;
window.fbAsyncInit = function() 
{
    FB.init
    (
        {
            appId  : 'appid',
            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)
       {
         console.log('Welcome!  Fetching your information.... ');
         FB.api('/me/friends/', function(response)
         {
                 for(i=0;i<20;i++)
                {
                    console.log('Good to see you, ' + response.data[i].name + '.');
                    console.log(response.data[i].id);
                    friendIds[i]=response.data[i].id;                   
                }
         });

       }
       else 
       {
         console.log('User cancelled login or did not fully authorize.');
       }
    });
};

   $(document).ready(function () 
   {

        $('#shareonfacebook').click(function(e) 
        {
            e.preventDefault();


                var publish = 

                {
                        method: 'stream.publish',
                        name: 'DvimayPostPhoto',
                        link: 'http://localhost/FaceBook/index.html',
                        picture: 'http://www.fbrell.com/public/f8.jpg',
                        caption: 'hey how is my Application ? tell me dude',
                        description: 'hey how is my Application ?',
                        from: 'myid',

                        message: ''
                };

            for(i=0;i<20;i++)
            {
                publish.target_id = friendIds[i];
                FB.ui(publish);
            }



            //publish.target_id = 'rupali.7';
            //FB.ui(publish);

        });
    });

</script>