How to generate Signature in AWS from Java : <p>When I invoke API endpoints from REST client, I got error by concerning with Signature.</p>

<p>Request:</p>

<blockquote>
  <p><strong>Host</strong>: <a href="https://xxx.execute-api.ap-southeast-1.amazonaws.com/latest/api/name" rel="noreferrer">https://xxx.execute-api.ap-southeast-1.amazonaws.com/latest/api/name</a></p>
  
  <p><strong>Authorization</strong>: AWS4-HMAC-SHA256 Credential=<code>{AWSKEY}</code>/20160314/ap-southeast-1/execute-api/aws4_request,SignedHeaders=host;range;x-amz-date,Signature=<code>{signature}</code></p>
  
  <p><strong>X-Amz-Date</strong>: 20160314T102915Z</p>
</blockquote>

<p>Response:</p>

<pre><code>{
"message": "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details. The Canonical String for this request should have been 'xxx' "
}
</code></pre>

<p>From Java code, I followed AWS reference of how to generate Signature.</p>

<pre><code>    String secretKey = "{mysecretkey}";
    String dateStamp = "20160314";
    String regionName = "ap-southeast-1";
    String serviceName = "execute-api";

    byte[] signature = getSignatureKey(secretKey, dateStamp, regionName, serviceName);
    System.out.println("Signature : " + Hex.encodeHexString(signature));

    static byte[] HmacSHA256(String data, byte[] key) throws Exception  {
         String algorithm="HmacSHA256";
         Mac mac = Mac.getInstance(algorithm);
         mac.init(new SecretKeySpec(key, algorithm));
         return mac.doFinal(data.getBytes("UTF8"));
    }

    static byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception  {
         byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
         byte[] kDate    = HmacSHA256(dateStamp, kSecret);
         byte[] kRegion  = HmacSHA256(regionName, kDate);
         byte[] kService = HmacSHA256(serviceName, kRegion);
         byte[] kSigning = HmacSHA256("aws4_request", kService);
         return kSigning;
    }
</code></pre>

<p>May I know what I was wrong while generating Signature?</p>

<p>Reference how to generate Signature : <a href="http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-java" rel="noreferrer">http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-java</a></p>
