如何使用spring social facebook从facebook获取组列表?

时间:2011-12-03 10:23:02

标签: facebook spring-social

我使用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以及如何获取所有组。

2 个答案:

答案 0 :(得分:0)

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

 @Controller

@RequestMapping( “/实/组”) 公共类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();
}

答案 1 :(得分:0)

您可以尝试以下代码从GroupOperations()

获取所有群组
List<GroupMembership> listGroup=facebook.groupOperations().getMemberships();