Micro
Micro

Reputation: 10891

How to sign AWS API request in Java (for Android app)

How can I sign an AWS API request in Java? I was able to find how to do this in PHP but cannot seem to find anything in Java. I would like to sign a request to ItemSearch.

Is there maybe a library or something? (Note: I want to use this for Android).

Upvotes: 0

Views: 887

Answers (3)

Shannon
Shannon

Reputation: 1163

AWS's SDKs provide various high-level clients for various services which automatically sign the requests.

However, if you're trying to sign an arbitrary request, you would most likely want to use the Aws4Signer class from the Java SDK 2. Here is an outline of using it to sign a SdkHttpFullRequest object, from which we can get the relevant headers.

public static Map<String, List<String>> generateAwsSignedHeaders(String host, String path, String httpMethod, String body)
    // Replace this with the relevant AWS service name
    String serviceName = "foo";
    String region = "us-east-1";

    Aws4Signer signer = Aws4Signer.create();

    // In this example we directly create a ProfileCredentialsProvider but normally you should have
    // a DefaultCredentialsProvider that you reuse
    ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
    SdkHttpFullRequest request = SdkHttpFullRequest.builder()
            .encodedPath(path)
            .protocol("https")
            .method(SdkHttpMethod.valueOf(httpMethod))
            .contentStreamProvider(() -> SdkBytes.fromUtf8String(body).asInputStream())
            .host(host)
            // Add headers here, if you want/need them to be signed
            //.putHeader("", "")
            .build();

    // Some use cases might require configuring a SignerChecksumParams too
    Aws4SignerParams signerParams = Aws4SignerParams.builder()
            .awsCredentials(credentialsProvider.resolveCredentials())
            .signingName(serviceName)
            .signingRegion(Region.of(region))
            .build();
    
    credentialsProvider.close();

    // Sign the request
    SdkHttpFullRequest signedRequest = signer.sign(request, signerParams);
    
    // In this example, we just return the headers which includes headers added by the signer
    return signedRequest.headers();
}

Upvotes: 0

WestonE
WestonE

Reputation: 702

I highly recommend using the AWS SDK for Android instead of trying to create requests manually.

Download: https://aws.amazon.com/mobile/sdk/ (Also supports Maven/Gradle) Api docs: http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/ Getting started:http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/Welcome.html GitHub: -Source => https://github.com/aws/aws-sdk-android -Samples => https://github.com/awslabs/aws-sdk-android-samples

Upvotes: 1

Rob
Rob

Reputation: 6497

Use the Java SDK. Unlike when using PHP, the Java library takes care of the signature for you automatically.

Example is here: http://docs.aws.amazon.com/AWSECommerceService/latest/GSG/ImplementinganA2SRequest.html

Upvotes: 1

Related Questions