Search code examples
springspring-socialspring-social-facebook

Cannot retrieve friends in Facebook API : Spring Social


I'm trying to retrieve friends' name of a user using Spring Social Facebook. List friends = facebook.friendOperations().getFriendProfiles(); This statement is giving me a 400 Bad Request error.

@Controller
@RequestMapping("/")
public class HelloController {

    private Facebook facebook;

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

    @RequestMapping(method=RequestMethod.GET)
    public String helloFacebook(Model model) {
        if (!facebook.isAuthorized()) {
            return "redirect:/connect/facebook";
        }

        model.addAttribute(facebook.userOperations().getUserProfile());
        System.out.println(facebook.userOperations().getUserProfile().getName());
        System.out.println(facebook.userOperations().getUserProfile().getEmail());
        //PagedList<FacebookProfile> fbFrnds = facebook.friendOperations().getFriendProfiles();
        List<String> friendIds = facebook.friendOperations().getFriendIds();
        System.out.println("Id size : "+friendIds.size());
        List<FacebookProfile> friends = facebook.friendOperations().getFriendProfiles();
        System.out.println("Size : "+friends.size());
        for (FacebookProfile id : friends) {
            System.out.println(id.getName());
        }
        return "hello";
    }

}

Error is shown below.

2014-10-20 16:39:34.787  WARN 4648 --- [nio-8080-exec-6] o.s.web.client.RestTemplate              : GET request for "https://graph.facebook.com/v1.0/me/friends?fields=id%2Cusername%2Cname%2Cfirst_name%2Clast_name%2Cgender%2Clocale%2Ceducation%2Cwork%2Cemail%2Cthird_party_id%2Clink%2Ctimezone%2Cupdated_time%2Cverified%2Cabout%2Cbio%2Cbirthday%2Clocation%2Chometown%2Cinterested_in%2Creligion%2Cpolitical%2Cquotes%2Crelationship_status%2Csignificant_other%2Cwebsite" resulted in 400 (Bad Request); invoking error handler
{"error":{"message":"(#100) Unknown fields: username.","type":"OAuthException","code":100}}
2014-10-20 16:39:34.814 ERROR 4648 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.social.UncategorizedApiException: (#100) Unknown fields: username.] with root cause

org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleUncategorizedError(FacebookErrorHandler.java:155)
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:68)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:598)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:556)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:527)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:259)
    at org.springframework.social.facebook.api.impl.FacebookTemplate.fetchConnections(FacebookTemplate.java:222)
    at org.springframework.social.facebook.api.impl.FriendTemplate.getFriendProfiles(FriendTemplate.java:135)
    at org.springframework.social.facebook.api.impl.FriendTemplate.getFriendProfiles(FriendTemplate.java:102)
    at hello.HelloController.helloFacebook(HelloController.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1695)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

I've given following permission in 'Configure app center permissions'. enter image description here

Any idea why this is happening? Thanks in advance.


Solution

  • Unfortunately, Facebook has removed the "username" property in v2.0 of their Graph API. Consequently, Spring Social Facebook will fail trying to fetch that property. (If that frustrates you, then imagine my frustration when I found out that they were removing it only days after cutting a Spring Social Facebook release.)

    If you change your Spring Social Facebook dependency to 2.0.0.M1 (and make sure you have Spring's milestone repository, http://repo.spring.io/milestone, registered in your build) then it should work for you. In fact, I'd be very appreciative if you'd try out this release and report bugs or improvement ideas to https://jira.spring.io/browse/SOCIALFB.

    But also be aware that Facebook's Graph API now limits applications on which friends they can fetch. Specifically, the Graph API will only return a list of a user's friends if those friends are also users of your application. I'm not sure if that will address your needs or not, but it's all due to changes on Facebook's end...there's not much I can do about it in Spring Social.