我在我的Google应用引擎应用程序中使用Amazon Web服务API.亚马逊已经表示他们将只接受2009年8月15日签署的请求.虽然他们已经给出了简单的签名指令,但我对SHA256的Python库知之甚少.应用程序引擎文档说它支持pycrypto,但我只是想知道(读取是懒惰的)如果有人已经这样做了.您可以分享的任何代码片段?我可能在这里遗失的任何问题?
以下是基于较低级别(然后是boto)库的REST请求示例.解决方案来自http://cloudcarpenters.com/blog/amazon_products_api_request_signing.
您所需要的只是AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY的有效条目
def amazon_test_url():
import base64, hashlib, hmac, time
from urllib import urlencode, quote_plus
AWS_ACCESS_KEY_ID = 'YOUR_KEY'
AWS_SECRET_ACCESS_KEY = 'YOUR_SECRET_KEY'
TEST_ISBN = '9780735619678' #http://stackoverflow.com/questions/1711/what-is-the-single-most-influential-book-every-programmer-should-read
base_url = "http://ecs.amazonaws.com/onca/xml"
url_params = dict(
Service='AWSECommerceService',
Operation='ItemLookup',
IdType='ISBN',
ItemId=TEST_ISBN,
SearchIndex='Books',
AWSAccessKeyId=AWS_ACCESS_KEY_ID,
ResponseGroup='Images,ItemAttributes,EditorialReview,SalesRank')
#Can add Version='2009-01-06'. What is it BTW? API version?
# Add a ISO 8601 compliant timestamp (in GMT)
url_params['Timestamp'] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
# Sort the URL parameters by key
keys = url_params.keys()
keys.sort()
# Get the values in the same order of the sorted keys
values = map(url_params.get, keys)
# Reconstruct the URL parameters and encode them
url_string = urlencode(zip(keys,values))
#Construct the string to sign
string_to_sign = "GET\necs.amazonaws.com\n/onca/xml\n%s" % url_string
# Sign the request
signature = hmac.new(
key=AWS_SECRET_ACCESS_KEY,
msg=string_to_sign,
digestmod=hashlib.sha256).digest()
# Base64 encode the signature
signature = base64.encodestring(signature).strip()
# Make the signature URL safe
urlencoded_signature = quote_plus(signature)
url_string += "&Signature=%s" % urlencoded_signature
print "%s?%s\n\n%s\n\n%s" % (base_url, url_string, urlencoded_signature, signature)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6778 次 |
| 最近记录: |