如何使用 Spring Social facebook 从 facebook 获取群组列表?

发布于 2024-12-19 03:24:56 字数 1851 浏览 3 评论 0原文

我已使用 Spring Social facebook api 将我的应用程序与 facebook 连接起来。我尝试使用以下方式获取所有组

FacebookTemplate facebookTemplate = new FacebookTemplate();     
List<Group> group = facebookTemplate.fetchConnections("me", "groups", Group.class);   

,但它会生成以下错误消息

    org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.
    org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:95)
    org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:60)
    org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)
    org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)
    org.springframework.web.client.RestTemplate.execute(RestTemplate.java:415)
    org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:213)
    org.springframework.social.facebook.api.impl.FacebookTemplate.fetchConnections(FacebookTemplate.java:180)
    org.springframework.social.facebook.api.impl.FacebookTemplate.fetchConnections(FacebookTemplate.java:174)
    com.horoppa.social.facebook.FacebookFeedController.postUpdate(FacebookFeedController.java:52)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)

It may be required to create FacebookTemplate object using AccessToken for authorization. 
    FacebookTemplate facebookTemplate = new FacebookTemplate(AccessToken);

请帮助我,如何获取 accessToken 以及如何获取所有组。

I have connected my app with facebook using spring social facebook api. I've tried to fetch all groups using following way

FacebookTemplate facebookTemplate = new FacebookTemplate();     
List<Group> group = facebookTemplate.fetchConnections("me", "groups", Group.class);   

But it generates the following error message

    org.springframework.social.MissingAuthorizationException: Authorization is required for the operation, but the API binding was created without authorization.
    org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:95)
    org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:60)
    org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)
    org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)
    org.springframework.web.client.RestTemplate.execute(RestTemplate.java:415)
    org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:213)
    org.springframework.social.facebook.api.impl.FacebookTemplate.fetchConnections(FacebookTemplate.java:180)
    org.springframework.social.facebook.api.impl.FacebookTemplate.fetchConnections(FacebookTemplate.java:174)
    com.horoppa.social.facebook.FacebookFeedController.postUpdate(FacebookFeedController.java:52)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)

It may be required to create FacebookTemplate object using AccessToken for authorization. 
    FacebookTemplate facebookTemplate = new FacebookTemplate(AccessToken);

Please help me, how to get accessToken and how to fetch all groups.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一萌ing 2024-12-26 03:24:56

您似乎正在使用 spring-social-showcase。因此,您可以通过以下方式获取当前用户的群组列表

 @Controller

@RequestMapping("/facebook/group")
public class FacebookGroupsController {

private final Facebook facebook;

@Inject
public FacebookGroupsController(Facebook facebook) {
    this.facebook = facebook;
}

/*
 * Fetch all group list
 */
@RequestMapping(value="/list", method=RequestMethod.GET)
public String showGroup(Model model) {              
    List<Group> groupList = facebook.fetchConnections("me", "groups", Group.class);         
    model.addAttribute("group", groupList);         
    return "facebook/groupList";
}

}

您还可以发现 SocialConfig.java 中已经为 facebook 创建了

@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public Facebook facebook() {
    Connection<Facebook> facebook = connectionRepository().findPrimaryConnection(Facebook.class);
    return facebook != null ? facebook.getApi() : new FacebookTemplate();
}

It seems that you are using spring-social-showcase. So, you can fetch group list of current user by following way

 @Controller

@RequestMapping("/facebook/group")
public class FacebookGroupsController {

private final Facebook facebook;

@Inject
public FacebookGroupsController(Facebook facebook) {
    this.facebook = facebook;
}

/*
 * Fetch all group list
 */
@RequestMapping(value="/list", method=RequestMethod.GET)
public String showGroup(Model model) {              
    List<Group> groupList = facebook.fetchConnections("me", "groups", Group.class);         
    model.addAttribute("group", groupList);         
    return "facebook/groupList";
}

}

You could also found that the been already created for facebook in SocialConfig.java

@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public Facebook facebook() {
    Connection<Facebook> facebook = connectionRepository().findPrimaryConnection(Facebook.class);
    return facebook != null ? facebook.getApi() : new FacebookTemplate();
}
素染倾城色 2024-12-26 03:24:56

您可以尝试下面的代码从 GroupOperations() 获取所有组

List<GroupMembership> listGroup=facebook.groupOperations().getMemberships();

You can try below code to get all Groups from GroupOperations()

List<GroupMembership> listGroup=facebook.groupOperations().getMemberships();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文