Java Spring社交-Facebook整合

Java Spring社交-Facebook整合,java,spring-social,spring-social-facebook,Java,Spring Social,Spring Social Facebook,我需要将spring social与Facebook集成,并从我的公司页面获取提要/帖子 代码片段 public static void main(String[] args) { Facebook facebook1 = new FacebookTemplate(""); FeedOperations feedOperations = facebook1.feedOperations(); PagedList<Post> feeds

我需要将spring social与Facebook集成,并从我的公司页面获取提要/帖子

代码片段

public static void main(String[] args) {
        Facebook facebook1 = new FacebookTemplate("");
        FeedOperations feedOperations = facebook1.feedOperations();
        PagedList<Post> feeds = feedOperations.getFeed();

        for (Post post : feeds) {
            System.out.println("@" + post.getName());
        }
    }
请在这方面帮助我

1.)在何处以及如何生成密钥

2.)如何访问用户源。我的密钥可能需要访问该用户配置文件

3.)我需要向用户提供


直接使用FacebookTemplate构造函数将为您提供一个未经身份验证(即,没有OAuth)的连接,该连接将用于不需要身份验证的查询。这就是为什么您会遇到授权异常,访问提要需要身份验证(一个经过身份验证的facebook用户,已经为您的应用程序提供了执行这些操作的正确授权)

请注意,您需要一种方法来获取用户的访问令牌以进行身份验证,在控制台应用程序中这样做可能会很棘手。然而,在Web应用程序中使用Spring Social非常简单,基本上您只需要

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

    private Facebook facebook;
    private ConnectionRepository connectionRepository;

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

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

        model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());
        PagedList<Post> feed = facebook.feedOperations().getFeed();
        model.addAttribute("feed", feed);
        return "hello";
    }

}
@控制器
@请求映射(“/”)
公共类Hello控制器{
私人脸书;
私有连接存储库连接存储库;
@注入
公共HelloController(Facebook、ConnectionRepository、ConnectionRepository){
this.facebook=facebook;
this.connectionRepository=connectionRepository;
}
@RequestMapping(method=RequestMethod.GET)
公共字符串helloFacebook(模型){
if(connectionRepository.findPrimaryConnection(Facebook.class)==null){
返回“重定向:/connect/facebook”;
}
model.addAttribute(“facebookProfile”,facebook.userOperations().getUserProfile());
PagedList feed=facebook.feedOperations().getFeed();
model.addAttribute(“提要”,提要);
回复“你好”;
}
}
(示例如下:) 正确配置了
spring.social.facebook.appId
spring.social.facebook.appSecret
的配置

不过,要使用控制台应用程序测试查询,您可以

  • 在Facebook的api页面上创建应用程序
  • FacebookTemplate
    -构造函数中使用该标记
  • 这将始终是您的用户,因此您可能不想将此应用程序发布到野外;)

    @Controller
    @RequestMapping("/")
    public class HelloController {
    
        private Facebook facebook;
        private ConnectionRepository connectionRepository;
    
        @Inject
        public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
            this.facebook = facebook;
            this.connectionRepository = connectionRepository;
        }
    
        @RequestMapping(method=RequestMethod.GET)
        public String helloFacebook(Model model) {
            if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
                return "redirect:/connect/facebook";
            }
    
            model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());
            PagedList<Post> feed = facebook.feedOperations().getFeed();
            model.addAttribute("feed", feed);
            return "hello";
        }
    
    }