javascriptfacebookfacebook-graph-apifacebook-javascript-sdkfb.ui

FB.ui() ignoring the "to" property


Has anyone experienced the situation where the "to" property of FB.ui gets ignored - even if it's set - and the post only goes to the wall of the currently logged in user?

When working with a completely stripped down test page that uses the FB sample code (and hard-coded parameter values) it posts to my wall as expected. But when I add "to" property to this FB code, it still only posts to my wall.

Any ideas would be greatly appreciated, as I have trawled these archives and the FB dev site in what I believe to be a thorough way.

The SDK loads properly and the iframe pops up (though it scrolls to the top of the page).

Here's the stripped down code used in my test page:

$("#well").click(function(e) {
  FB.ui(
  {
   method: 'feed',
   to: '***', /* <=== this person is a confirmed friend with appropriate privacy settings */
   name: 'The Facebook SDK for Javascript',
   caption: 'Bringing Facebook to the desktop and mobile web',
   description: (
      'A small JavaScript library that allows you to harness ' +
      'the power of Facebook, bringing the user\'s identity, ' +
      'social graph and distribution power to your site.'
   ),
   link: 'https://developers.facebook.com/docs/reference/javascript/',
   picture: 'http://www.fbrell.com/public/f8.jpg'
  },
  function(response) {
    if (response && response.post_id) {
      alert('Post was published.');
    } else {
      alert('Post was not published.');
    }
  }
);

Thanks in advance.

# # UPDATE # #

Thanks to Sahil Mittal's contributions I worked out this fairly reasonable solution:

`

var jqxhr = $.get( "http://graph.facebook.com/"+$("#fbto").val()+"?fields=id", function( data ) {
 // could do something useful here... 
})

.done(function(  data ) {
  // do something else useful useful, in this case finish posting stuff to the wall 
})

.fail(function(  data ) {
 // thrown if the user doesn't exist
})

.always(function(  data ) {
  // etc... 
})

`

Now, this is a jQuery 1.8.* solution, and I should also add that in a high-traffic scenario I'm not sure how kindly FB would take to hammering on their URL to convert submitted usernames to ids. But that's a bridge I'll happily cross if we get there!

Thanks again to Sahil.


Solution

  • You cannot use username in the to parameter. You have to use id instead.

    If you dont have the id, you can get that from the username itself. Make the call /{username}?fields=id and you'll get the id in response.

    For eg: http://graph.facebook.com/sahilmittal?fields=id.

    The response is:

    {
      id: "595273610"
    }
    

    Just get this id and use it in the to parameter.