Search code examples
javaspringspring-bootfacebook-graph-apiphoto

How to get user photo from facebook


When I register a user through facebook in spring-boot aplication, how to get his photo from facebook right away? I need get current user photo and set this photo to database


Solution

  • After facebook Login success using Spring social library get the profile picture.

    @Controller
    @RequestMapping("/")
    public class MainController {
    
        private Facebook facebook;
        private ConnectionRepository connectionRepository;
    
        public MainController(Facebook facebook, ConnectionRepository connectionRepository) {
            this.facebook = facebook;
            this.connectionRepository = connectionRepository;
        }
    
        @RequestMapping(value = "feed", method = RequestMethod.GET)
        public String feed(Model model) {
    
            if(connectionRepository.findPrimaryConnection(Facebook.class) == null) {
                return "redirect:/connect/facebook";
            }
    
            byte[] profilePicture = facebook.userOperations().getUserProfileImage();
            User userProfile = facebook.userOperations().getUserProfile();
            model.addAttribute("userProfile", userProfile);
            PagedList<Post> userFeed = facebook.feedOperations().getFeed();
            model.addAttribute("userFeed", userFeed);
            return "feed";
        }
    }