How to generate Signature in AWS from Java

前端 未结 6 645
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 10:19

When I invoke API endpoints from REST client, I got error by concerning with Signature.

Request:

Host: https://xxx.execute-ap

6条回答
  •  长情又很酷
    2020-12-24 10:55

    The easiest way is to use methods and http-client from Amazon's SDK. I follow the below 3 steps.

    Step1: Create basic AWS credentials:

     BasicAWSCredentials awsCreds = new BasicAWSCredentials(ACCESS_KEY,AWS_DATASHOP_SECRET_KEY);
    

    Step2: Create signableRequest:

    DefaultRequest signableRequest = new DefaultRequest<>("aws-service-name");
        signableRequest.setHttpMethod(HttpMethodName.GET);
        signableRequest.setResourcePath("fooo");
        signableRequest.setEndpoint(URI.create(baar));
        signableRequest.addParameter("execution_id", executionId);
        signableRequest.addHeader("Content-Type", "application/json");
    
        signer.sign(signableRequest, awsCreds);
    

    Step3: Execute request using AmazonHttpClient:

    new AmazonHttpClient(new ClientConfiguration())
                        .requestExecutionBuilder()
                        .executionContext(new ExecutionContext(true))
                        .request(signableRequest)
                        .errorResponseHandler((new SimpleAwsErrorHandler()))
                        .execute(new MyResponseHandler());
    

    Make sure to implement HttpResponseHandler for SimpleAwsErrorHandler and MyResponseHandler

    If you want to use normal http clients, you would have to create a canonical request and calculate signature which most often doesn't match.

提交回复
热议问题