Facebook Javascript SDK – Post to Wall

After logged in to Facebook, we can post message to the logged in user account with hyperlink. Try the following code.

<html>
  <head>
    <title>My Facebook Login Page</title>
    <script type="text/javascript">
      function postToWall() {
        var params = {};
        params['message'] = 'Visit Eureka! - https://ykyuen.wordpress.com';
        params['name'] = 'Eureka!';
        params['link'] = 'https://ykyuen.wordpress.com';
        params['picture'] = 'http://1.gravatar.com/blavatar/9c99f3c76686bf5edf1ea460356f00f2?s=128&ts=1321625806';
        params['description'] = 'Hope my posts are useful to you. =)';
        
        FB.api('/me/feed', 'post', params, function(response) {
          if (!response || response.error) {
            alert('Error occured');
          } else {
            alert('Post ID: ' + response.id);
          }
        });
      }
    </script>
  </head>
  <body>
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
      FB.init({ 
        appId:<fbapp-id>, cookie:true, 
        status:true, xfbml:true
      });
    </script>
    <fb:login-button perms="publish_stream">Login with Facebook</fb:login-button>
    <a href="javascript:postToWall()">Post To Wall</a>
  </body>
 </html>

 

Replace the fbapp-id with yours and try it out. you will get the following wall post.

 

Done =)

Reference: Facebook Developers – Facebook FB.api post how to specify a link target

6 thoughts on “Facebook Javascript SDK – Post to Wall”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.