将curl请求转换为aws cognito到java http请求
我正在尝试转换(cognito): 通过java11网络库连接到java:将curl请求转换为aws cognito到java http请求,java,curl,amazon-cognito,Java,Curl,Amazon Cognito,我正在尝试转换(cognito): 通过java11网络库连接到java: public void loginHttp(username,password,clientId) throws IOException, InterruptedException { HttpClient httpClient = HttpClient.newHttpClient(); Map<Object,Object> credentialsMap = new HashMap<&g
public void loginHttp(username,password,clientId) throws IOException, InterruptedException {
HttpClient httpClient = HttpClient.newHttpClient();
Map<Object,Object> credentialsMap = new HashMap<>();
credentialsMap.put("USERNAME",username);
credentialsMap.put("PASSWORD",password);
Map<Object,Object> requestBody = new HashMap<>();
requestBody.put("AuthFlow","USER_PASSWORD_AUTH");
requestBody.put("ClientId",clientId);
requestBody.put("AuthParameters",credentialsMap);
HttpRequest request = HttpRequest.newBuilder(URI.create("https://cognito-idp.eu-west-1.amazonaws.com"))
.header("X-Amz-Target","AWSCognitoIdentityProviderService.InitiateAuth")
.header("Content-Type","application/x-amz-json-1.1")
.POST(HttpRequest.BodyPublishers.ofString(requestBody.toString()))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.toString());
}
public void loginHttp(用户名、密码、客户端ID)引发IOException、InterruptedException{
HttpClient HttpClient=HttpClient.newHttpClient();
Map credentialsMap=新的HashMap();
credentialsMap.put(“用户名”,USERNAME);
credentialsMap.put(“密码”,PASSWORD);
Map requestBody=new HashMap();
put(“AuthFlow”、“USER\u PASSWORD\u AUTH”);
requestBody.put(“ClientId”,ClientId);
put(“AuthParameters”,credentialsMap);
HttpRequest请求=HttpRequest.newBuilder(URI.create(“https://cognito-idp.eu-west-1.amazonaws.com"))
.header(“X-Amz-Target”、“AWSCognitoIdentityProviderService.InitiateAuth”)
.header(“内容类型”、“应用程序/x-amz-json-1.1”)
.POST(HttpRequest.bodypublisher.ofString(requestBody.toString()))
.build();
HttpResponse response=httpClient.send(请求,HttpResponse.BodyHandlers.ofString());
System.out.println(response.toString());
}
当通过邮递员发送请求时,我得到的是回复(返回代码200),但当我使用java时,我得到的是返回代码400(错误请求)
我遗漏了什么吗?看起来像是myMap。toString()以字符串形式生成映射,但不包含
键和值周围的引号:
{
key1 : value1
key2 : value2
}
因此,我使用Jackson的对象映射器将贴图转换为正确的表示:
{
"key1" : "value1"
"key2" : "value2"
}
代码:
ObjectMapper objectMapper = new ObjectMapper();
String stringMap = objectMapper.writeValueAsString(requestBody)
HttpRequest request = HttpRequest.newBuilder(URI.create("https://cognito-idp.eu-west-1.amazonaws.com/"))
.header("X-Amz-Target","AWSCognitoIdentityProviderService.InitiateAuth")
.header("Content-Type","application/x-amz-json-1.1")
.POST(HttpRequest.BodyPublishers.ofString(stringMap))
.build();
(请注意POST方法args中使用的对象映射器)
ObjectMapper objectMapper = new ObjectMapper();
String stringMap = objectMapper.writeValueAsString(requestBody)
HttpRequest request = HttpRequest.newBuilder(URI.create("https://cognito-idp.eu-west-1.amazonaws.com/"))
.header("X-Amz-Target","AWSCognitoIdentityProviderService.InitiateAuth")
.header("Content-Type","application/x-amz-json-1.1")
.POST(HttpRequest.BodyPublishers.ofString(stringMap))
.build();