views:

58

answers:

1

I've tried to use FB.api to post something to my feed for hours now. I can't get it to work for me. I gave the permissions to the app. I can post to my feed with the PHP SDK but I have to use JavaScript.

<button onclick="doPost()">Post to Stream</button>

<script>
window.doPost = function() {
  FB.api(
    '/me/feed',
    'post',
    { body: 'Trying the Graph' },
    Log.info.bind('/me/feed POST callback')
  );
};
</script>

Can someone give me the example of a simple HTML page that uses FB.api to post to a feed?

A: 

Well, I got it working myself. I'm not sure what was wrong the first time as I started from scratch with a new HTML file. I hope it will help someone:

    <!DOCTYPE html>
    <html xmlns:fb="http://www.facebook.com/2008/fbml"&gt;
    <head>
    </head>
    <body>

    <a href="#" onClick="postToFacebook()">Post to Facebook</a>

    <script>
    function postToFacebook() {
        var body = 'Reading Connect JS documentation';

        FB.api('/me/feed', 'post', { body: body, message: 'My message is ...' }, function(response) {
          if (!response || response.error) {
            alert('Error occured');
          } else {
            alert('Post ID: ' + response);
          }
        });
    }
    </script>

    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId  : 'YOUR APP ID GOES HERE',
          status : true, // check login status
          cookie : true, // enable cookies to allow the server to access the session
          xfbml  : true  // parse XFBML
        });
      };

      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>

    </body>
    </html>
Fair Dinkum Thinkum