[英]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. 我正在尝试与Instagram的API通信,但我从请求中得到的回复却表明未检测到传递给主体的参数。
{"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. 我试图通过传递一个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;
}
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" ) 尝试通过终端上的curl命令或chrome上的Rested插件(其中“内容类型”设置为“ application / x-www-form-urlencoded”,并且已放置参数)将相同的请求完美传递时,在“请求正文”中)
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. ps:我也在寻找一种方法来检索从我的请求中接收到的值并将其存储在变量中,而不是将其返回给客户端。
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: 在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");
}
});
Hope this helps you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.