Amazon Product Advertising API (ItemSearch with ItemPage)

Question:

I wrote the following code:

from hashlib import sha256
from base64 import b64encode
import hmac
import urllib
from time import strftime, gmtime

url = 'http://ecs.amazonaws.com/onca/xml'
AWSAccessKeyId = amazon_settings.amazon_access_key_id
AssociateTag = amazon_settings.amazon_associate_tag
Keywords = urllib.quote_plus('Potter')
Operation = 'ItemSearch'
SearchIndex = 'Books'
Service = 'AWSECommerceService'
Timestamp = urllib.quote_plus(strftime("%Y-%m-%dT%H:%M:%S.000Z", gmtime()))
Version = '2011-08-01'

sign_to = 'GETnecs.amazonaws.comn/onca/xmlnAWSAccessKeyId=%s&AssociateTag=%s&Keywords=%s&Operation=%s&SearchIndex=%s&Service=%s&Timestamp=%s&Version=%s' % (AWSAccessKeyId, AssociateTag, Keywords, Operation, SearchIndex, Service, Timestamp, Version)

Signature = urllib.quote_plus(b64encode(hmac.new(str(amazon_settings.amazon_secret_access_key), str(sign_to), sha256).digest()))

request = '%s?AWSAccessKeyId=%s&AssociateTag=%s&Keywords=%s&Operation=%s&SearchIndex=%s&Service=%s&Timestamp=%s&Version=%s&Signature=%s' % (url, AWSAccessKeyId, AssociateTag, Keywords, Operation, SearchIndex, Service, Timestamp, Version, Signature)

print request

When i use this code all fine.
But if i try add ItemPage param to sign_to variable and to request variable i get error SignatureDoesNotMatch.

Help me please.

Asked By: user1427111

||

Answers:

It’s actualy not answer to you question, but i recomend you take a look at excellent python wrapper for the Amazon Product Advertising API – python-amazon-product-api

Answered By: Sergey Lyapustin

It’s hard to find in the documentation, but you have to make sure that your list of Operations are in alphabetical order or else you get a SignatureDoesNotMatch error.

For example, ItemPage must go between AssociateTag and Keywords to be valid.

AWSAccessKeyId
AssociateTag
ItemPage
Keywords
Operation
ResponseGroup
SearchIndex
Service
SignatureVersion
Timestamp
Version
Answered By: kylex