如何使用 Spring 社交获取 Facebook 页面的活动

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

我的目标是检索特定 Facebook 页面和用户配置文件的事件列表。列表应该只有 return 个将来的事件。我似乎找不到使用 Spring 社交 Facebook API 的方法。我坚持使用以下代码。

使用 Spring 社交 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";
    }

}

我使用更灵活的 RestOperations API 解决了这个问题。

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 的回答之外,这里还有一个 EventData class 的简单实现(只是一个包含 Event 对象的列表):

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

此外,我发现需要在 json 响应中指定字段才能显示多个标准集:

https://graph.facebook.com/v2.12/[pageid]/events?
fields=id,name,message,description,place,start_time,end_time,picture,cover