我试图找回朋友'使用Spring Social Facebook的用户名。 列出friends = 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)
我已在“配置应用中心权限”中获得以下权限。
知道为什么会这样吗? 提前谢谢。
答案 0 :(得分:6)
不幸的是,Facebook已经删除了他们的Graph API v2.0中的“用户名”属性。因此,Spring Social Facebook将无法尝试获取该属性。 (如果这让你感到沮丧,那么当我发现他们在切割Spring社交Facebook版本后几天才删除它时,想象一下我的沮丧。)
如果您将Spring社交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中我无能为力。