1

我正在尝试与 Instagram 的 API 通信,但我从我的请求中得到的回复说我传递给身体的参数没有被检测到。

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

我尝试通过在 .post() 中传递 JsonNode 或字符串来发送请求,如下所示,但两者均不成功。

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 插件(其中“内容类型”设置为“应用程序/x-www-form-urlencoded”并放置参数)时,相同的请求完美传递在“请求正文”内)

有谁知道我应该如何发送这个请求?


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

4

1 回答 1

0

看来您缺少一个:

.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");
                }

            });

希望这对您有所帮助。

于 2018-11-19T04:15:57.063 回答