繁体   English   中英

播放框架2.6-Java:通过oAuth Instagram请求POST

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

我正在尝试与Instagram的API通信,但我从请求中得到的回复却表明未检测到传递给主体的参数。

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

我试图通过传递一个JsonNode或.post()内的字符串来发送请求,如下所示,但是两者都失败了。

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;
}

尝试通过终端上的curl命令或chrome上的Rested插件(其中“内容类型”设置为“ application / x-www-form-urlencoded”,并且已放置参数)将相同的请求完美传递时,在“请求正文”中)

有人对我应该如何发送此请求有任何想法吗?


ps:我也在寻找一种方法来检索从我的请求中接收到的值并将其存储在变量中,而不是将其返回给客户端。

看来您缺少:

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

请看下面的代码。 在post()中,您还可以使用Json对象,以便发送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");
                }

            });

希望这对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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