5

I am looking for examples of the java code used to make a call to an AWS API Gateway.

I can make the call to an AWS API gateway that dose not require IAM verification but I am finding it very difficult to find a clear example of how to structure a URL.

How do you pass in the Secret key and Secret Key ID appropriately? Do I have to complete all the steps required to encode the URL in accordance with Signature Version 4 protocol or can I just include the key and key ID with the URL?

Is there a code example of a client side program, written in Java, that uses the AWS SDK to make a call to an AWS API Gateway? As in a generic call in which you can pass in the URL and other parameters as they are required?

I have tried to use the AWS Java SDK that is generated by the AWS API Gateway. I am having trouble using the generated SDK any tutorials would be greatly appreciated. I can't find documentation on how to use the constructed SDK to make a call to the API. I have used MAVEN to build the SDK.. I think, I was trying to follow this tutorial...

http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-call-apigateway-generated-java-sdk.html

.... but I am unsure if I have installed the SDK correctly and I can't see where the secret key and key ID are passed in to the API call...

bloopiebloopie
  • 309
  • 1
  • 4
  • 11
  • Also see the alternative [generic Java SDK](http://blog.ryang.ca/2017/05/31/generic-amazon-api-gateway-java-client-sdk/) which can take any credential provider. – RyanG May 31 '17 at 21:16
  • 100% agree. Amazon's documentation on this use-case is garbage. I have submitted negative feedback, hopefully they'll fix it soon. – Ring Jun 24 '22 at 22:17

2 Answers2

1
You should sign your request and credentials as per AWS Signature V4. 

Sample code to sign is available @ https://s3.amazonaws.com/aws-java-sdk/samples/AWSS3SigV4JavaSamples.jar

Sample code : 
URL url = new URL("https://XXXXXXX.execute-api.us-west-2.amazonaws.com/dev/pets");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("host", "XXXXXXXXXX.execute-api.us-west-2.amazonaws.com");


         Map<String, String> headers = new HashMap<String, String>();
            headers.put("x-amz-content-sha256", AWS4SignerBase.EMPTY_BODY_SHA256);
            headers.put("X-Amz-Security-Token", "XXXXXX+XXXXXXXXXXXXXXX/5N+XXXXXXXXX/XXXXX/XXXXXX/XXXXXXXX/XXXXXXXX=");
            AWS4SignerForAuthorizationHeader signer = new AWS4SignerForAuthorizationHeader(
                    url, "GET", "execute-api", "us-west-2");
            String authorization = signer.computeSignature(headers, 
                                                           null, // no query parameters
                                                           AWS4SignerBase.EMPTY_BODY_SHA256, 
                                                           "XXXXXX", 
                                                               "S+XXXXXX+XXXXXX/XXXX");



            headers.put("Authorization", authorization);
            String response = HttpUtils.invokeHttpRequest(url, "GET", headers, null);
Siva Kumar
  • 21
  • 2
  • Hello I found this code very useful, anyway I'm trying to upload an image and can't get an authorization tokent caluclated correctly when uploading binary data, do you have perhaps some example also for that ? Tks – pavants Feb 28 '23 at 11:29
-1

You should be able to initial the SDK client with credential and pass in a credential provider, like,

BasicAWSCredentials awsCreds = new BasicAWSCredentials("access_key_id", "secret_key_id");
SimpleCalcSdk sdkClient = SimpleCalcSdk.builder()
                         .withCredentials(awsCreds)
                         .build();
Ka Hou Ieong
  • 5,835
  • 3
  • 20
  • 21