title: How can I calculate an AWS API signature (v4) in python?
body: <p>I'm attempting to generate a signature for an Amazon Glacier upload request, using the <a href="http://docs.amazonwebservices.com/amazonglacier/latest/dev/amazon-glacier-signing-requests.html#example-signature-calculation" rel="noreferrer">example requests</a> and <a href="http://docs.amazonwebservices.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-python" rel="noreferrer">example functions</a> provided by the AWS documentation, but I can't make it work. At this point, I'm certain I'm missing something incredibly obvious:</p>
  <pre><code>#!/bin/env python
  
  import hmac
  import hashlib
  
  # This string to sign taken from: http://docs.amazonwebservices.com/amazonglacier/latest/dev/amazon-glacier-signing-requests.html#example-signature-calculation
  sts = &quot;&quot;&quot;AWS4-HMAC-SHA256
  20120525T002453Z
  20120525/us-east-1/glacier/aws4_request
  5f1da1a2d0feb614dd03d71e87928b8e449ac87614479332aced3a701f916743&quot;&quot;&quot;
  
  # These two functions taken from: http://docs.amazonwebservices.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-python
  def sign(key, msg):
      return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).hexdigest()
  
  # The fake secret key is provided by the referenced docs
  def getSignatureKey():
      kDate = sign((&quot;AWS4&quot; + &quot;wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY&quot;).encode('utf-8'), &quot;20120525&quot;)
      kRegion = sign(kDate, &quot;us-east-1&quot;)
      kService = sign(kRegion, &quot;glacier&quot;)
      kSigning = sign(kService, &quot;aws4_request&quot;)
      return kSigning
  
  signature = sign(getSignatureKey(), sts)
  print signature
  </code></pre>
  <p>If I run my program, I get the following hash:</p>
  <pre><code>$ python test.py
  3431315da57da4df28f92895c75364d94b36c745896ad3e580c0a6ae403b1e05
  </code></pre>
  <p>Yet the docs clearly state:</p>
  <blockquote>
  <p>If the secret access key, wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY, is used, then the calculated signature is:</p>
  <p>3ce5b2f2fffac9262b4da9256f8d086b4aaf42eba5f111c21681a65a127b7c2a</p>
  </blockquote>
  <p>What am I missing?</p>