如何从Python应用引擎签署亚马逊网络服务请求? [英] How to sign amazon web service requests from the python app engine?

查看:75
本文介绍了如何从Python应用引擎签署亚马逊网络服务请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google应用引擎应用程序中使用Amazon web服务api。亚马逊曾表示,他们只会接受2009年8月15日以前的签名请求。虽然他们已经提供了简单的说明进行签名,我对SHA256的Python库知之甚少。应用引擎文档表示它支持pycrypto,但我只是想知道(读懒)如果有人已经这样做。任何代码片段,你可以分享?我在这里可能会缺少的任何问题?

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 simple instructions for signing, I am not so knowledgeable of Python libraries for SHA256. The app engine documentation says it supports pycrypto but I was just wondering (read being lazy) if anyone has already done this. Any code snippets you could share? Any issues I might be missing here?

推荐答案

基于 http://jjinux.blogspot.com/2009/06/python-amazon-product- advertising-api.html
这是一个小改进的版本,可让您在拨打电话之前将特定于呼叫的参数字典与基本参数合并。

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 = '<your-key>'
    AWS_ASSOCIATE_TAG = '<your-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 ...

这篇关于如何从Python应用引擎签署亚马逊网络服务请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆