1

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

}
Siya Sosibo
  • 338
  • 1
  • 10
  • 23

2 Answers2

2

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

}
Siya Sosibo
  • 338
  • 1
  • 10
  • 23
  • Afaik the only way to get a list of Event objects is to call `EventOperations.getCreated()` which returns a list for the currently connected user. But to get Events for any other page or user the framework oddly enough offers no solution. So I guess your proposed solution is the way to go. – Richard Osseweyer Jan 23 '18 at 13:16
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
Richard Osseweyer
  • 1,693
  • 20
  • 25