Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get FB.api('/me/feed', 'post', ... to work?

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?

like image 422
Jan Deinhard Avatar asked Oct 14 '10 12:10

Jan Deinhard


People also ask

How do I get Facebook posts on API?

Your app needs user_posts permission from the person who created the post or the person tagged in the post. Then your app can read: Timeline posts from the person who gave you the permission. The posts that other people made on that person Timeline.

How do I share a Facebook post with API?

Yes, you can share using the graph2 api. The way you do it is to use /feed edge and pass the post's url that you want to share as the link. Standard Fb permissions to the post you are sharing do apply. This was done today, in a local rails app, using FbGraph2 gem, with the above method.

How does an API work with Facebook?

The Facebook API verifies your credentials & generates a unique authentication token & passes it to the third-party app, verifying the login process. Graph API is an HTTP based API via which apps can post on users walls, upload photos, share events & stuff.


2 Answers

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">
    <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>
like image 77
Jan Deinhard Avatar answered Sep 19 '22 16:09

Jan Deinhard


I use this code on fb game-app and looks like this http://trupa.files.wordpress.com/2012/04/prscreenan.jpg

<a href="#" onClick="publishStory();" class="sendFeed"><br><font style="color:#FFF; text-decoration:none;padding-left:27px;">post to wall</font></a><br>
<script>
function publishStory() {
  FB.ui({
    method: 'feed',
    name: 'message name',
    caption: 'message caption ',
    description: 'description goes here',
    link: 'the url current page',
    picture: 'if you want to add an image'
  }, 
  function(response) {
    console.log('publishStory response: ', response);
  });
  return false;
}
</script>
like image 36
Dan N Avatar answered Sep 23 '22 16:09

Dan N