jeudi 26 février 2015

Amazon Product Advertising API Signature

I am trying to produce a signature for the Amazon Product Advertising API, been at it a few hours and am still getting a 403 - could anyone have a quick look at the code and tell me if I am doing anything wrong please?


This is the function I use to create the signature



def create_signature(service, operation, version, search_index, keywords, associate_tag, time_stamp, access_key):
start_string = "GET\n" + \
"webservices.amazon.com\n" + \
"/onca/xml\n" + \
"AWSAccessKeyId=" + access_key + \
"&AssociateTag=" + associate_tag + \
"&Keywords=" + keywords + \
"&Operation=" + operation + \
"&SearchIndex=" + search_index + \
"&Service=" + service + \
"&Timestamp=" + time_stamp + \
"&Version=" + version

dig = hmac.new("MYSECRETID", msg=start_string, digestmod=hashlib.sha256).digest()
sig = urllib.quote_plus(base64.b64encode(dig).decode())

return sig;


And this is the function I use to return the string for the request



def ProcessRequest(request_item):
start_string = "http://ift.tt/1f5d9Nk?" + \
"AWSAccessKeyId=" + request_item.access_key + \
"&AssociateTag=" + request_item.associate_tag + \
"&Keywords=" + request_item.keywords + \
"&Operation=" + request_item.operation + \
"&SearchIndex=" + request_item.search_index + \
"&Service=" + request_item.service + \
"&Timestamp=" + request_item.time_stamp + \
"&Version=" + request_item.version + \
"&Signature=" + request_item.signature
return start_string;


And this is the run code



_AWSAccessKeyID = "MY KEY"
_AWSSecretKey= "MY SECRET KEY"

def ProduceTimeStamp():
time = datetime.datetime.now().isoformat()
return time;

item = Class_Request.setup_request("AWSECommerceService", "ItemSearch", "2011-08-01", "Books", "harry%20potter", "PutYourAssociateTagHere", ProduceTimeStamp(), _AWSAccessKeyID)
item2 = Class_Request.ProcessRequest(item)


An example web request it spits out that produces at 403 is this:-



http://ift.tt/1vDMqW7


There is also a holder class called ClassRequest that just has a field for every request field


The instructions I followed are here if anyone is intrested:- http://ift.tt/1joWDuw


I hope someone can help, I am new to Python and a bit lost





Aucun commentaire:

Enregistrer un commentaire