Retrieving a Facebook user's profile

In this recipe, you'll learn how to retrieve a Facebook user's profile data, which automatically becomes available to the app once the user has authorized it.

Getting ready

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

How to do it…

Here are the steps to retrieve the profile of a Facebook user:

  1. In the FacebookController class, add a Model argument to the fb() method:
    @RequestMapping("/fb")
    public String fb(HttpServletRequest request, Model model) {
    ...
  2. In the if(facebook.isAuthorized()) block, use the Facebook object to retrieve the user's profile:
    FacebookProfile profile = facebook.userOperations().getUserProfile(); 
  3. Pass the user profile to the JSP view:
    model.addAttribute("profile", profile);  ...

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.