2

I have AWSCredentials object includes accesskey, sessiontoken, etc. However, although it seems very simple, i couldn't find any document how i can call simple http request(For example to Api Gateway service) using these credentials. I tried AmazonWebServiceClient, AmazonHttpClient, etc.

How can i call simple http post or get request using aws credentials and get response?

Arda Güçlü
  • 728
  • 2
  • 6
  • 27

2 Answers2

2

This code block solved my problem and thanks to it, i can send simple signed http requests.

BasicSessionCredentials credentials = new BasicSessionCredentials(accessKey, secretKey, sessionToken);

            AmazonWebServiceRequest amazonWebServiceRequest = new AmazonWebServiceRequest() {
            };

            ClientConfiguration clientConfiguration = new ClientConfiguration();

            Request request = new DefaultRequest(amazonWebServiceRequest,Constants.API_GATEWAY_SERVICE_NAME);
            request.setEndpoint(URI.create(posturl));
            request.setHttpMethod(HttpMethodName.POST);

            AWS4Signer signer = new AWS4Signer();
            signer.setServiceName(Constants.API_GATEWAY_SERVICE_NAME);
            signer.setRegionName(Region.getRegion(Regions.US_WEST_2).getName());
            signer.sign(request, credentials);

            AmazonCustomWebClient webClient = new AmazonCustomWebClient(clientConfiguration, mContext);
            webClient.Execute(request, new HttpResponseHandler<AmazonWebServiceResponse<String>>() {
                @Override
                public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception {
                    return null;
                }

                @Override
                public boolean needsConnectionLeftOpen() {
                    return false;
                }
            });

API_GATEWAY_SERVICE_NAME constant is important for api gateway request it is 'execute-api', and AmazonCustomWebClient is derived from AmazonWebServiceClient.

Arda Güçlü
  • 728
  • 2
  • 6
  • 27
-1

What you are trying to do is called signing the AWS API requests.

This documentation has all details: http://docs.aws.amazon.com/general/latest/gr/signing_aws_api_requests.html

But you don't need to sign requests manually if you are using the SDK.

Quoting from the documentation:

If you are using one of the AWS SDKs, the AWS Command Line interface (CLI), or a service-specific CLI, you do not need to worry about signing requests. All you need to do is configure the tools with one or more access keys. These tools construct and send requests to AWS for you, and as part of that process, they sign the requests using an access key that you provide. They take care of many of the connection details, such as calculating signatures, handling request retries, and error handling.

patanjal
  • 645
  • 4
  • 4
  • No, I surveyed this document detailly, however, there is no clue how i can use in Android, which classes are necessary. There is only some fundamental basics. – Arda Güçlü Mar 14 '16 at 07:17