春季社交facebook accessToken有400错误

时间:2014-12-05 11:34:18

标签: spring-social

嗨,请帮助我对Spring社交新手,我从AUTHORIZATION_CODE获取访问令牌时遇到400错误。

我的代码如下

@RequestMapping(value = "/facebook", method = RequestMethod.POST, produces = "application/json")
  @ResponseBody
  public Object getFacebookLoginPage(@RequestBody SocialCommand socialCommand) throws Exception {
      loggerService.debug("In ShareController", "getFacebookLoginPage method for facebook", "returns the JSON response for the input socialCommand");
      Result result = new Result();
      result.status = "OK";
      dataObject = socialCommand;
      FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory(msgprop.getProperty("facebook.appId"), msgprop.getProperty("facebook.appSecrete"), msgprop.getProperty("facebook.namespace"));
      oAuth2Operations = connectionFactory.getOAuthOperations();
      OAuth2Parameters params = new OAuth2Parameters();
      params.setScope(msgprop.getProperty("facebook.scope"));
      params.setRedirectUri(msgprop.getProperty("facebook.redirectURI"));
//        params.set("Content-Type", MediaType.MULTIPART_FORM_DATA.getType());
      String authorizeUrl = oAuth2Operations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, params);
      System.out.println(authorizeUrl);
      SuccessResponse successResponse = new SuccessResponse();
      successResponse.resultObj = authorizeUrl;
      result.response = successResponse;
      return result;

  }

并且回调控制器如下

  @RequestMapping(value = "/facebook/callback", params = "code", method = RequestMethod.GET)
  public Object faceBookCallback(@RequestParam(value = "code") String callBackCode, Model model) throws IOException {
      loggerService.debug("In ShareController", "faceBookCallback method for facebook", "returns the JSON response for the input socialCommand");
      MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
      formData.add("client_id", msgprop.getProperty("facebook.appId"));
      formData.add("client_secret", msgprop.getProperty("facebook.appSecrete"));
      formData.add("scope", msgprop.getProperty("facebook.scope"));
      formData.add("redirect_uri", msgprop.getProperty("facebook.redirectURI"));
      formData.set("grant_type", "authorization_code");
      formData.set("Content-Type", MediaType.MULTIPART_FORM_DATA.getType());
      AccessGrant accessGrant = oAuth2Operations.exchangeForAccess(callBackCode, msgprop.getProperty("facebook.redirectURI"), formData);
      System.out.println(accessGrant.getAccessToken());
      appStatus.getActivityId();
      SocialCommand socialCommand = (SocialCommand) dataObject;
      socialCommand.setAppType("facebook");
      socialCommand.setAccessToken(accessGrant.getAccessToken());
      getImageLocation(socialCommand);

      model.addAttribute("activityId", appStatus.getActivityId());

      return "backToViewDetails";
  }

oAuth2Operations.exchangeForAccess提供400错误请求

1 个答案:

答案 0 :(得分:0)

我注意到的一些事情......

首先,为什么要编写自己的控制器来执行OAuth舞蹈而不是使用Spring Social提供的ConnectController? ConnectController可以为您处理所有重定向,并从一开始就是Spring Social的一部分。 Spring Social Showcase(https://github.com/spring-projects/spring-social-samples/tree/master/spring-social-showcase)是使用ConnectController的项目示例。

即便如此,假设您有充分的理由编写自己的连接控制器,我在getFacebookLoginPage()中看不到执行实际重定向到Facebook的任何内容,因此我只能假设在未显示的代码中发生。然后,当重定向从Facebook返回时,我很困惑为什么在调用exchangeForAccess()之前设置内容类型和大多数参数。 (即使这样,您还要设置看似请求标题的内容,但是您将其设置为表单参数...不是必需的,但也不是您想到的。)

再次,我建议你看一下ConnectController(https://github.com/spring-projects/spring-social/blob/master/spring-social-web/src/main/java/org/springframework/social/connect/web/ConnectController.java)。它可能会做你需要它做的事情。即使你仍然觉得需要编写自己的控制器,你可以看看ConnectController如何作为如何编写自己的控制器的指南。