Mayank
Mayank

Reputation: 5738

Facebook OAuth Exception: redirect_uri missing

I'm new to JAVA and using play framework to create a website. I'm trying to integrate Facebook oauth into my website. The code that I have is:

// This function is called to populate _facebook, _facebookAuthParams and
// _facebookTokenParams. The first two are HashMaps and _facebookTokenParams is 
// ObjectNode
private static void _initFacebook() {

    String _url = "shutterdeck.com:8080";

    _facebook.put("authUrl", "https://graph.facebook.com/oauth/authorize");
    _facebook.put("tokenUrl", "https://graph.facebook.com/oauth/access_token");
    _facebookAuthParams.put("scope", "email");
    _facebookAuthParams.put("responseType", "code");
    _facebookAuthParams.put("redirect_uri", _url + "/oauth/facebookRedirect");
    _facebookAuthParams.put("client_id", "..");
    _facebookTokenParams.put("grant_type", "authorization_code");
    _facebookTokenParams.put("redirect_uri", _url + "/oauth/facebookRedirect");
    _facebookTokenParams.put("scope", "email");
    _facebookTokenParams.put("client_id", "...");
    _facebookTokenParams.put("client_secret", "...");
}

The function below tries to fetch the access_token from facebook:

public static Result facebookRedirect() {
    Map<String, String[]> query = request().queryString();
    if (query.containsKey("code")) {
        String[] value = query.get("code");
        String code = value[0];
        if (value.length > 1 || code.isEmpty())
            return redirect("/");

        String tokenUrl = _getTokenUrl(_facebook);
        _facebookTokenParams.put("code", code);
        Promise<WS.Response> promise = WS.url(tokenUrl).post(_facebookTokenParams);
        Function<WS.Response, Result> f = new Function<WS.Response, Result>() {
            public Result apply(WS.Response response) {
                    System.out.println(response.asJson()); // This prints an error json from face book
                    String token = response.asJson().get("token").asText();
                    _getFacebookUserInfo(token);
                    return ok("/");
            }
        };
        return async(promise.map(f));
    }
    return ok("404");
}

The error that I'm getting from Facebook is:

{"error":{"message":"Missing redirect_uri parameter.","type":"OAuthException","code":191}}

I verified that redirect_url is present in _facebookTokenParams which is passed to post. What could be something I'm missing here? Moreover, how could I view the POST request which is being sent to Facebook?

Upvotes: 0

Views: 1313

Answers (2)

Clay Coleman
Clay Coleman

Reputation: 371

For some reason, the Facebook Graph API endpoint wants us to pass the client_id, client_secret, and the grant_type parameters in the URL as url encoded parameters. That means no spaces on the grant_type – just a single comma between the credentials!

Upvotes: 2

Marco
Marco

Reputation: 1493

You have to pass the parameters in querystring.

Upvotes: 0

Related Questions