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:
    @RequestMapping("/fb")
    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(); ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.