Facebook OAuth错误:验证验证码时出错

时间:2016-04-21 15:32:02

标签: java facebook-graph-api oauth facebook-apps restfb

我试图获取Facebook登录的访问令牌,但却收到了错误。我发现的所有答案都告诉我错误的redirect_uri格式。 这是我得到的错误:

{
   "error": {
      "message": "Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request",
      "type": "OAuthException",
      "code": 100,
      "fbtrace_id": "Ht12b5BKgRK"
   }
}

这是Facebook App中注册的重定向URI。 enter image description here

我做的步骤:

  1. https://graph.facebook.com/oauth/authorize?client_id=APP_ID&redirect_uri=http://127.0.0.1:8080/MyProject/Mapping&scope=user_posts&response_type=code
  2. 此链接将我重定向至http://127.0.0.1:8080/MyProject/Mapping?code=GENERATED_CODE
  3. 然后在代码中生成下一个URI: https://graph.facebook.com/oauth/access_token?client_id=549422435210997&redirect_uri=http://127.0.0.1:8080/MyProject/Mapping&client_secret=CLIENT_SECRET&code=GENERATED_CODE
  4. 请求此URI会给我错误,而不是access_token
  5. 我也试过redirect_uri=http://127.0.0.1:8080/MyProject/Mapping/。仍然没有结果。

    另外,我使用restFB库为Java绑定了相同的步骤。并得到了同样的错误。

    ScopeBuilder scopeBuilder = new ScopeBuilder();
    scopeBuilder.addPermission(UserDataPermissions.USER_POSTS);
    FacebookClient client = new DefaultFacebookClient(Version.LATEST);
    String loginDialogUrlString = client.getLoginDialogUrl(APP_ID, "http://127.0.0.1:8080/MyProject/Mapping", scopeBuilder);
    System.out.println(loginDialogUrlString);
    
    System.out.println();
    AccessToken appAccessToken = client.obtainAppAccessToken(APP_ID, APP_SECRET);
    System.out.println(appAccessToken.getAccessToken());
    System.out.println(appAccessToken.getTokenType());
    
    //On this step i got the same error
    AccessToken userAccessToken = client.obtainUserAccessToken(APP_ID, APP_SECRET, "http://127.0.0.1:8080/MyProject/Mapping/", appAccessToken.getAccessToken());
    System.out.println(userAccessToken.getAccessToken());
    

1 个答案:

答案 0 :(得分:0)

当我这样做时,有一点让我感到震惊的是确保redirect_uri是URL编码的。在Java中,我的代码做了类似的事情:

import java.net.URLEncoder;

URLEncoder.encode("http://127.0.0.1:8080/MyProject/Mapping", "UTF-8") 

用于我的重定向网址。不要编码整个网址,只是你的redirect_uri部分。