How to sign amazon web service requests from the python app engine?

后端 未结 6 1712
清歌不尽
清歌不尽 2021-02-06 18:53

I use Amazon web service api from within my Google app engine application. Amazon have said that they will only accept signed requests from Aug 15, 2009. While they have given

6条回答
  •  忘掉有多难
    2021-02-06 19:36

    Got this to work based on code sample at http://jjinux.blogspot.com/2009/06/python-amazon-product-advertising-api.html Here is a minor improved version that lets you merge a dict of call specific params with the basic params before making the call.

    keyFile = open('accesskey.secret', 'r')
    # I put my secret key file in .gitignore so that it doesn't show up publicly
    AWS_SECRET_ACCESS_KEY = keyFile.read()
    keyFile.close()
    
    def amz_call(self, call_params):
    
        AWS_ACCESS_KEY_ID = ''
        AWS_ASSOCIATE_TAG = ''
    
        import time
        import urllib
        from boto.connection import AWSQueryConnection
        aws_conn = AWSQueryConnection(
            aws_access_key_id=AWS_ACCESS_KEY_ID,
            aws_secret_access_key=Amz.AWS_SECRET_ACCESS_KEY, is_secure=False,
            host='ecs.amazonaws.com')
        aws_conn.SignatureVersion = '2'
        base_params = dict(
            Service='AWSECommerceService',
            Version='2008-08-19',
            SignatureVersion=aws_conn.SignatureVersion,
            AWSAccessKeyId=AWS_ACCESS_KEY_ID,
            AssociateTag=AWS_ASSOCIATE_TAG,
            Timestamp=time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime()))
        params = dict(base_params, **call_params)
        verb = 'GET'
        path = '/onca/xml'
        qs, signature = aws_conn.get_signature(params, verb, path)
        qs = path + '?' + qs + '&Signature=' + urllib.quote(signature)
        print "verb:", verb, "qs:", qs
        return aws_conn._mexe(verb, qs, None, headers={})
    

    Sample usage:

    result = self.amz_call({'Operation' : 'ItemSearch' , 'Keywords' : searchString , 'SearchIndex' : 'Books' , 'ResponseGroup' : 'Small' })
    if result.status == 200:
        responseBodyText = result.read()
        # do whatever ...
    

提交回复
热议问题