skip to Main Content

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";
    }

}

2

Answers


  1. Chosen as BEST ANSWER

    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";
        }
    
    }
    

  2. In addition to Siya’s answer here’s a simple implementation of the EventData class (just a List holding the Event objects):

    import java.util.List;
    import org.springframework.social.facebook.api.Event;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class EventData {
    
        private List<Event> data;
    
        public List<Event> getData() {
            return data;
        }
    }
    

    Furthermore, I found that one needs to specify the fields in the json response for more than a standard set to appear:

    https://graph.facebook.com/v2.12/[pageid]/events?
    fields=id,name,message,description,place,start_time,end_time,picture,cover
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search