gpt4 book ai didi

java - Spring Social - Facebook 集成

转载 作者:行者123 更新时间:2023-11-30 07:25:40 26 4
gpt4 key购买 nike

我需要将 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());
}
}

我已经在“https://developers.facebook.com ”上生成了 key ,但是当我在 FacebookTemplate(""); 中输入相同的 key 并运行程序时,它会抛出错误。

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.

2.) How can i get access to user feeds. My secret key might need to get access to that user profile.

3.) Where i need to provide user

最佳答案

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

请注意,您将需要一种方法来对用户的访问 token 进行身份验证,并且在控制台应用程序中执行此操作可能会很棘手。然而,在 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";
}

}

(示例取自此处: https://github.com/spring-guides/gs-accessing-facebook )正确配置了 spring.social.facebook.appIdspring.social.facebook.appSecret 的配置。

但是,要使用控制台应用程序测试您的查询,您可能

  1. 在 Facebook 的 API 页面上创建应用。
  2. 转到https://developers.facebook.com/tools/accesstoken/
  3. FacebookTemplate-构造函数中使用该 token

这将永远是您的用户,因此您可能不想将此应用程序发布到野外;)

关于java - Spring Social - Facebook 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36840857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com