views:

304

answers:

2

Hi there, I've gotten the FB JS client library to work on my iframe-based application (cool stuff, I must say), but now I'd like to retrieve a list of friends from Facebook, in order to populate an AutoComplete field (similar to the friend-selector), but without the additional iframe that would be generated if I used the serverfbml tag.

Anyone know how to retrieve that friend list?

A: 

Is the iFrame piece your talking about the Facebook-provided 'share with friends' facility? If so, you'll probably find that a lot easier to use than writing your own. If you write your own, here's what you'll need to do:

If you're strictly on the client side (i.e. JS, not PHP, Java, etc.), make JavaScript calls to the Friends API to get an XML list of friends back. Use JavaScript to parse through the XML and generate a list. You'll have to either write or 'borrow' some code to make that list usable with one or more 'autocomplete' fields. You then need to post that form + fields back to a share API -- many ways to do that, depending on what you're trying to accomplish.

There may already be well-written JS code / plugins that you could use. Do a quicks search and see if you can find something.

Josh
+1  A: 
<script src="http://static.ak.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
<script type="text/javascript">
  FB_RequireFeatures(['Api'], OnFBFeaturesLoaded);

  function OnFBFeaturesLoaded()
  {
    FB.Facebook.init('<YOUR_API_KEY>', '<XD_RECEIVER_URL>');
    FB.Facebook.apiClient.requireLogin(OnRequireLoginComplete);
  }

  function OnRequireLoginComplete(p_exception)
  {
    var FQL = "SELECT uid FROM user WHERE " +
              "uid IN (SELECT uid2 FROM friend WHERE uid1='<USER_ID_OF_CURRENT_USER>')";

    FB.Facebook.apiClient.fql_query(FQL, function(result, exception)
    {
      if (exception != null)
      {
        // We had some error, do your error handling stuff
      }
      else
      {
        // result is an array containing the friends' user IDs.
        // Have fun.
      }
    });
  }
</script>
Lior Cohen
DUDE, AWESOME! Thanks!
btelles
Good luck with your app :)
Lior Cohen

related questions