Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

List<GroupMembership> listGroup=facebook.groupOperations().getMemberships();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top