Quantcast

Signing Amazon Web Services API Requests in Python

I wanted to ping the “Amazon Product Advertising API” which now requires an HMAC signature, and the pyAWS library doesn’t sign requests and is no longer maintained. Here is some Python code to create a signed request:

# pyAWS no longer works with the AWS signed request requirement
# Sign an AWS REST request using the method described here
# http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?RequestAuthenticationArticle.html
#_______________________________________________________________________________
def getSignedUrl(accessKey, secretKey, params):
 
    #Step 0: add accessKey, Service, Timestamp, and Version to params
    params['AWSAccessKeyId'] = accessKey
    params['Service']        = 'AWSECommerceService'
 
    #Amazon adds hundredths of a second to the timestamp (always .000), so we do too.
    #(see http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html)
    params['Timestamp']      = time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime())
    params['Version']        = '2009-03-31'
 
    #Step 1a: sort params
    paramsList = params.items()
    paramsList.sort()
 
    #Step 1b-d: create canonicalizedQueryString
    # This code comes from http://blog.umlungu.co.uk/blog/2009/jul/12/pyaws-adding-request-authentication/
    # and the resulting discussion
    canonicalizedQueryString = '&'.join(['%s=%s' % (k,urllib.quote(str(v))) for (k,v) in paramsList if v])
 
    #Step 2: create string to sign
    host          = 'ecs.amazonaws.com'
    requestUri    = '/onca/xml'
    stringToSign  = 'GET\n'
    stringToSign += host +'\n'
    stringToSign += requestUri+'\n'
    stringToSign += canonicalizedQueryString.encode('utf-8')
 
    #Step 3: create HMAC
    digest = hmac.new(secretKey, stringToSign, hashlib.sha256).digest()
 
    #Step 4: base64 the hmac
    sig = base64.b64encode(digest)
 
    #Step 5: append signature to query
    url  = 'http://' + host + requestUri + '?'
    url += canonicalizedQueryString + "&Signature=" + urllib.quote(sig)
 
    return url

San Francisco Food Bank

TikiRobot is donating $100 to the SF Food Bank this winter. They feed 132,000 people every year, and this year their dry good supplies are down 15%. If you are in town over the holidays, they need volunteers.

Every time we get a check from Amazon for having their widget in our sidebar, we’re rounding it up to $100 and donating to a worthy cause.


cc by-nc picture by Veronica Belmont

WiiMe

wiime1.jpg
They sent me 9 consecutive text messages this morning, alternating between “Wiis are in stock at Amazon! Grab one!” and “No Wiis available :-( they were up for 8 seconds.

I’d pretty much given up until the 9th msg came through and I got one! yay! I can’t believe that it’s been a year already and it’s *STILL* so hard to get a Wii on Amazon. Yes, I know it’s possible to get one at a store if you live in Missouri (or just not in the Bay Area)…or if you wait in line at Best Buy at the crack of dawn, but I really didn’t want to do that (cause I’m lazy). I just wanted to get one online for the office and WiiMe came through!