Facebook iFrame app and JS: how to get friends through JS Client

facebook

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?

Best Solution

<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>
Related Question