1

我正在尝试使用 Spring Social Facebook 检索用户的朋友姓名。列出好友 = facebook.friendOperations().getFriendProfiles(); 该语句给了我一个 400 Bad Request 错误。

@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";
    }

}

错误如下所示。

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)

我在“配置应用中心权限”中授予了以下权限。 在此处输入图像描述

知道为什么会这样吗?提前致谢。

4

1 回答 1

6

不幸的是,Facebook 在其 Graph API 的 v2.0 中删除了“用户名”属性。因此,Spring Social Facebook 将无法尝试获取该属性。(如果这让你感到沮丧,那么想象一下当我发现他们在删除 Spring Social Facebook 版本后仅几天就将其删除时我的沮丧。)

如果您将 Spring Social Facebook 依赖项更改为 2.0.0.M1(并确保您在构建中注册了 Spring 的里程碑存储库http://repo.spring.io/milestone),那么它应该适合您。事实上,如果您试用此版本并向https://jira.spring.io/browse/SOCIALFB报告错误或改进想法,我将非常感激。

但也请注意,Facebook 的 Graph API 现在限制了应用程序可以获取哪些朋友。具体来说,如果这些朋友也是您的应用程序的用户,Graph API 只会返回用户朋友的列表。我不确定这是否能满足您的需求,但这完全是由于 Facebook 端的变化……在 Spring Social 中我无能为力。

于 2014-10-21T15:08:04.763 回答