gpt4 book ai didi

google-app-engine - 如何签署来自 python 应用程序引擎的亚马逊网络服务请求?

转载 作者:行者123 更新时间:2023-12-04 09:20:01 25 4
gpt4 key购买 nike

我在我的 Google 应用引擎应用程序中使用 Amazon 网络服务 API。亚马逊表示他们只接受从 2009 年 8 月 15 日起签名的请求。虽然他们给出了简单的 instructions对于签名,我不太了解 SHA256 的 Python 库。应用引擎文档 says它支持 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)

关于google-app-engine - 如何签署来自 python 应用程序引擎的亚马逊网络服务请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1088715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com