facebook-graph-apispring-bootspring-social-facebook

How to get a Facebook Page's events with Spring Social


My goal is to retrieve a list of events for a certain Facebook page and user profile. List should only return events in the future. I can't seem to find a way with Spring Social Facebook API. I'm stuck with the following code.

Using Spring Social Facebook v3.0.0.M1

@Controller
@RequestMapping("/")
public class HelloController {

    private Facebook facebook;
    private ConnectionRepository connectionRepository;

    public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
        this.facebook = facebook;
        this.connectionRepository = connectionRepository;
    }

    @GetMapping
    public String helloFacebook(Model model) {
        if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
            return "redirect:/connect/facebook";
        }

        //Should  only return events in the future
        PagingParameters pagingParameters = new PagingParameters(10,0,0L,0L);
        PagedList<Event> userEvents = facebook.eventOperations().search("noCriteriaNeed",pagingParameters);
        PagedList<Event> pageEventfacebooks = facebook.pageOperations().facebookOperations("pageID").eventOperations().search("noCriteriaNeed",pagingParameters);


        model.addAttribute("UserfacebookEvents", userEvents);
        model.addAttribute("PagefacebookEvents", pageEventfacebooks);
        return "hello";
    }

}

Solution

  • I solved this by using the RestOperations API which is more flexible.

    package com.housescent.almanac.web.controller;
    
    import com.housescent.almanac.web.model.EventData;
    import org.springframework.social.connect.ConnectionRepository;
    import org.springframework.social.facebook.api.Facebook;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/")
    public class HelloController {
    
        private Facebook facebook;
        private ConnectionRepository connectionRepository;
    
        public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
            this.facebook = facebook;
            this.connectionRepository = connectionRepository;
        }
    
        @GetMapping
        public String helloFacebook(Model model) {
            if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
                return "redirect:/connect/facebook";
            }
    
            EventData userEvents = facebook.restOperations().getForObject("https://graph.facebook.com/v2.8/pageid/events",EventData.class);
    
    
            model.addAttribute("PagefacebookEvents", userEvents);
            return "hello";
        }
    
    }