Spring 社交 Facebook UncategorizedApiException: (#3) 应用程序没有进行此 API 调用的能力

Spring Social Facebook UncategorizedApiException: (#3) Application does not have the capability to make this API call

我开发了一个简单的 Web 应用程序,它通过 Spring Boot (v1.2.6.RELEASE) 和 Spring Social Facebook (v2.0.[=47) 访问 Facebook 数据=]) 类似于给出的示例 here

我在 Facebook 中创建了一个使用 Graph API 2.5 版的新应用。

根据这个例子,我修改了 facebookConnect.html 将这个不同的范围放入方法 POST 请求:

<html>
<head>
    <title>Facebook Extractor</title>
</head>
<body>
    <h3>Connect to Facebook</h3>

    <form action="/recommender/connect/facebook" method="POST">
        <input type="hidden" name="scope" value="public_profile, user_friends, email, user_likes" />
        <div class="formInfo">
            <p>You aren't connected to Facebook yet. Click the button to connect this application with your Facebook account.</p>
        </div>
        <p><button type="submit">Connect to Facebook</button></p>
    </form>
</body>

由启动 OAuth 授权代码流的 ConnectController 处理...

OAuth 成功(已授予权限)并完成连接后,出现以下错误:

(#3) application does not have the capability to make this api call.

这里是控制器的主要部分,它处理来自 Facebook 应用程序的重定向:

@Controller

@Scope(值="session") public class FacebookExtractor {

private Logger logger = Logger.getLogger(FacebookExtractor.class);

private Facebook facebook;
@Autowired
GraphDatabase graphDatabase;
@Autowired
PersonRepository personRepository;
@Autowired
UserProfileRepository userProfileRepository;
@Autowired
AttributeDefinitionRepository attributeDefinitionRepository;
@Autowired
AttributeRepository attributeRepository;
@Autowired
ConceptRepository conceptRepository;
@Autowired
RecommenderGovConsumerRepository recGovRepository;
@Autowired
GovConsumerInfluencedByGovConsumerRelationshipRepository influenceRepository;
@Autowired
ExtractorListener listener;

private String userBind;

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

@RequestMapping(method = RequestMethod.GET, value = "/facebookExtractor")
public ModelAndView FacebookDataUserExtraction() {
    if (!facebook.isAuthorized()) {
        return new ModelAndView("redirect:/recommender/connect/facebook");
    }
    String account=userBind;
    Long netId = null;
    Transaction tx = graphDatabase.beginTx();
    try {
        User user = facebook.userOperations().getUserProfile();
        Person p = savePerson(user);
        UserProfile up = saveUserProfile(user);
        up.setUser(p);
        userProfileRepository.save(up);
        p.setProfile(up);
        saveSocialInteractions(up);
        saveSocialPreferences(up);
        personRepository.save(p);
        netId=p.getId();
        tx.success();
    } finally {
        tx.close();
    }
    if (account != null) {
        /*
         * Binding di Facebook riuscito.
         */
        listener.onProfileDataCompleted(netId, SNAccountType.Facebook);
        listener.onInteractionsCompleted(netId, InteractionType.Friendship, SNAccountType.Facebook);
        return new ModelAndView("redirect:http://localhost:8080/portal",
                "accountF", account);
    } else {
        //vista opportuna per estrazione avvenuta senza bind
        return new ModelAndView("facebookNoBindExtraction");
    }
}

观察堆栈跟踪错误,似乎我在控制器的这一行遇到错误:

User user = facebook.userOperations().getUserProfile();

但这很奇怪,因为默认情况下我应该有权获取基本用户配置文件数据。

注意:与 Facebook 中使用 Graph API 2.3 版的旧应用程序相同的代码可以完美运行,但我需要一个新的用于不同的目的并且不能强制使用新的Facebook 中的应用程序使用该版本...

这是我得到的堆栈错误....

12:01:16.953 [http-nio-9080-exec-5] ERROR 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: (#3) Application does not have the capability to make this API call.] with root cause org.springframework.social.UncategorizedApiException: (#3) Application does not have the capability to make this API call. at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:91) ~[spring-social-facebook-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:59) ~[spring-social-facebook-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:614) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:570) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:545) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:253) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.social.facebook.api.impl.FacebookTemplate.fetchObject(FacebookTemplate.java:214) ~[spring-social-facebook-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.social.facebook.api.impl.FacebookTemplate.fetchObject(FacebookTemplate.java:209) ~[spring-social-facebook-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.social.facebook.api.impl.UserTemplate.getUserProfile(UserTemplate.java:53) ~[spring-social-facebook-2.0.2.RELEASE.jar:2.0.2.RELEASE] at org.springframework.social.facebook.api.impl.UserTemplate.getUserProfile(UserTemplate.java:49) ~[spring-social-facebook-2.0.2.RELEASE.jar:2.0.2.RELEASE] at it.cerict.recommender.extractor.controllers.FacebookExtractor.FacebookDataUserExtraction(FacebookExtractor.java:89) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60] at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.0.26.jar:8.0.26] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at it.cerict.recommender.config.CORSFilter.doFilter(CORSFilter.java:22) ~[classes/:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) ~[tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) [tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) [tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) [tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) [tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526) [tomcat-embed-core-8.0.26.jar:8.0.26] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482) [tomcat-embed-core-8.0.26.jar:8.0.26] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_60] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_60] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.26.jar:8.0.26] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_60]

问题出在

中的 PROFILE_FILEDS
public User getUserProfile(String facebookId) {
  return graphApi.fetchObject(facebookId, User.class, PROFILE_FIELDS);
}

数组 PROFILE_FIELDS 包含许多字段,据我正确理解 spring-社交文档,Facebook 仅返回您有权查看的这些字段。 但现在情况发生了变化:

$curl "https://graph.facebook.com/v2.5/me?access_token=[access_tokens]&fields=[all_the_fields_from_PROFILE_FIELDS]
{"error":{"message":"(#3) Application does not have the capability to make this API call.","type":"OAuthException","code":3,"fbtrace_id":"Antivssjj1d"}}

$ curl "https://graph.facebook.com/v2.3/me?access_token=[access_token]&fields=id,about"
{"id":"1135898619755433"}

与此同时,我找到了解决方案。相反

User profile = facebook.userOperations().getUserProfile()

我们可以使用

User profile = facebook.fetchObject("me", User.class, "id", "name", "link", "email");

//Note: facebook = org.springframework.social.facebook.api.Facebook
//Note: User.class = org.springframework.social.facebook.api.User.class

或者等待 spring-social-facebook 的更新,存储库中有一个 pull request 待处理:https://github.com/spring-projects/spring-social-facebook/pull/171

更新: 使用 2.0.3.RELEASE 版本为我修复。

即使在添加了我的应用所需的 publish_actions 作用域之后,我仍然遇到此错误。我的问题是我需要 Facebook 来审查我的应用程序。如果您的应用是在 2014 年 4 月 30 日之后创建的,并且满足以下一项或多项条件,则属于这种情况:

  • 您希望您的应用出现在 Facebook 应用中心
  • 您想创建新的或编辑现有的 Open Graph 操作和故事
  • 您的应用要求用户提供任何扩展权限,包括 publish_actions

查看 Facebook Login Review FAQs 了解有关审核流程的更多信息。