将curl请求转换为AWS Cognito转换为Java HTTP请求

问题描述

我正在尝试转换following curl request(对aws cognito进行身份验证):

curl --location --request POST 'https://cognito-idp.us-west-1.amazonaws.com/' \
--header 'X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth' \
--header 'Content-Type: application/x-amz-json-1.1' \
--data-raw '{
    "AuthFlow": "USER_PASSWORD_AUTH","AuthParameters": {
        "PASSWORD": "pw","USERNAME": "user"
    },"ClientId": "the_cognito_client_id"
}'
通过Java11网络库

到Java:

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

通过邮递员发送请求时,我会收到响应(返回码200),但是当我使用Java时,我会得到返回码400(错误请求)。

我想念什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)