Book Image


Book Image


Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
About the Author
About the Reviewers

Retrieving the list of friends of a Facebook user

In this recipe, you'll learn how to retrieve the friends list of a Facebook user from a Spring web application.

Getting ready

This recipe uses the code from the Connecting to Facebook recipe.

How to do it…

Here are the steps to retrieve the list of friends of a Facebook user:

  1. In the FacebookController class, in the login() method, add user_friends to the scope parameter:

      params.setScope("public_profile, user_friends");
  2. Add a Model argument to the fb() method:

    public String fb(HttpServletRequest request, Model model) {
  3. In the if(facebook.isAuthorized()) block, use the Facebook object to get the list of friends:

    List<Reference> friendList = facebook.friendOperations().getFriends();
  4. Retrieve the profile of each friend:

    List<FacebookProfile> friendProfileList = new LinkedList<FacebookProfile>();
    for (Reference friend : friendList) {
      FacebookProfile friendProfile = facebook.userOperations().getUserProfile(friend...