簡體   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