1
votes

I'm trying to get the access token for Facebook login, but got an error instead. All the answers that I found tell me about the wrong redirect_uri format. This is the error I got:

{
   "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"
   }
}

And here is the redirect URI registered in Facebook App. enter image description here

Steps that I do:

  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. This link redirect me to http://127.0.0.1:8080/MyProject/Mapping?code=GENERATED_CODE
  3. Then in the code i generate next 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. Requesting this URI gives me error instead of access_token

I have tryied also redirect_uri=http://127.0.0.1:8080/MyProject/Mapping/. Still no results.

Also, I have tied the same steps using restFB library for Java. And got the same error.

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

1 Answers

1
votes

The one thing that bit me when I did this is to make sure that the redirect_uri is URL encoded. In Java my code did something like:

import java.net.URLEncoder;

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

for my redirect URL. Don't encode the entire URL, just the portion that is your redirect_uri.