简体   繁体   English

Spring Social - Facebook 集成

[英]Spring Social - Facebook integration

I need to integrate spring-social with Facebook, and get the feeds/post from my company page.我需要将 spring-social 与 Facebook 集成,并从我的公司页面获取提要/帖子。

Code snippet代码片段

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

I have generated the key on " https://developers.facebook.com ", but when i enter the same in FacebookTemplate("");我已经在“ https://developers.facebook.com ”上生成了密钥,但是当我在FacebookTemplate(""); , and runs the program , it throws me an error. ,并运行该程序,它向我抛出一个错误。

Exception in thread "main" org.springframework.social.InvalidAuthorizationException: Invalid OAuth access token.
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:81)
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:59)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:616)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:572)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:547)

Please help me in this regard,请在这方面帮助我,

1.) Where and how can i generate keys. 1.) 我在哪里以及如何生成密钥。

2.) How can i get access to user feeds. 2.) 我怎样才能访问用户提要。 My secret key might need to get access to that user profile.我的密钥可能需要访问该用户配置文件。

3.) Where i need to provide user 3.) 我需要向用户提供的地方

Using the FacebookTemplate constructor directly will provide you with an unauthenticated (that is, without OAuth) connection only, that will work for queries that do not require authentication.直接使用 FacebookTemplate 构造函数只会为您提供未经身份验证(即没有 OAuth)的连接,这将适用于不需要身份验证的查询。 This is why you get Authorization Exceptions, accessing feeds require authentication (an authenticated facebook user, that has given your application the proper authorization to perform these actions).这就是您获得授权异常的原因,访问提要需要身份验证(经过身份验证的 Facebook 用户,它已为您的应用程序提供了执行这些操作的正确授权)。

Note that you will need a way to get the user's access token to be authenticated, and doing so in a console application might prove tricky.请注意,您需要一种方法来验证用户的访问令牌,而在控制台应用程序中这样做可能会很棘手。 Using Spring Social in a WebApplication however is quite easy, basically all you need is然而,在 WebApplication 中使用 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";
    }

}

(example taken from here: https://github.com/spring-guides/gs-accessing-facebook ) with a configuration with spring.social.facebook.appId and spring.social.facebook.appSecret configured properly. (示例取自此处: https : //github.com/spring-guides/gs-accessing-facebook )具有正确配置spring.social.facebook.appIdspring.social.facebook.appSecret配置。

To test your queries with a console application however, you might但是,要使用控制台应用程序测试您的查询,您可能

  1. Create app on Facebook's api pages.在 Facebook 的 api 页面上创建应用程序。
  2. Go to https://developers.facebook.com/tools/accesstoken/转到https://developers.facebook.com/tools/accesstoken/
  3. Use that tokens in the FacebookTemplate -ConstructorFacebookTemplate -Constructor 中使用该令牌

This will always be your user, so you might not want to release this app into the wild ;)这将始终是您的用户,因此您可能不想将此应用程序发布到野外;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM