简体   繁体   中英

Play framework 2.6 - Java : Ws request POST with oAuth Instagram

I'm trying to communicate with Instagram's API but the reply I get back from my request says that the parameters I passed onto the body weren't detected.

{"error_type":"OAuthException","code":400,"error_message":"You must provide a client_id"}

I tried to send the request by passing a JsonNode or a string inside .post(), like below, but both where unsuccessful.

public CompletionStage<Result> getInstagramToken() {
    String code = request().getQueryString("code");
    if(code != null) {
        WSRequest request = ws.url("https://api.instagram.com/oauth/access_token").setContentType("application/x-wwww-form-urlencoded");

        // Json body
        /*JsonNode body = Json.newObject()
                         .put("client_id", insta_clientId)
                         .put("client_secret", insta_clientSecret)
                         .put("grant_type", "authorization_code")
                         .put("redirect_uri", redirect_uri)
                         .put("code", code);*/

        // String body
        String body = "client_id="+insta_clientId+"&client_secret="+insta_clientSecret+"&grant_type=authorization_code&redirect_uri="+redirect_uri+"&code="+code;

        CompletionStage<WSResponse> response = request.post(body);

        return response.thenApplyAsync(resp -> ok(resp.asJson()), exec);
    }
    return null;
}

The same request passed flawlessly when trying to send it by using a curl command on a terminal or with the Rested plugin on chrome ( where "content type" is set to "application/x-www-form-urlencoded" and the parameters are placed inside "Request body" )

Does anyone have any idea as to how I am supposed to send this request ?


ps: I am also looking for a way to retrieve the value received from my request and store it in a variable instead of returning it to the client.

It seems you are missing a:

.setContentType("application/x-www-form-urlencoded")

Look at our code below. In the post() you can also use a Json object so you can send a HashMap:

CompletionStage<Result> out = ws.url(cbUrl)
            .setAuth(<<your user>> , <<your password>>, WSAuthScheme.BASIC)
            .setRequestTimeout(Duration.ofSeconds(5))
            .setContentType("application/x-www-form-urlencoded")
            .post("param=value")
            .handle((response, error) -> {

                // Was the Chargebee API successful?
                if (error == null) {

                    // Debugging purposes
                    JsonNode jn = response.asJson();
                    Logger.debug(Json.toJson(postMap).toString());
                    Logger.debug(jn.toString());

                    // Success stuff

                    return ok("good");

                } else {

                    // Error stuff

                    return ok("bad");
                }

            });

Hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM