签署SimpleDB请求

2024-09-27 00:17:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python调用Amazon的SimpleDB服务。例如,我使用最简单的请求,ListDomains。但是,不管我怎么尝试,得到的响应总是“我们计算的请求签名与您提供的签名不匹配。”

这是我要签名的字符串(根据documentation here):

GET
https://sdb.amazonaws.com/
/
AWSAccessKeyId=<redacted>&Action=ListDomains&SignatureMethod=HmacSHA1&SignatureVersion=2&Timestamp=2011-04-19T18%3A50%3A43&Version=2009-04-15

我用以下代码签名:

^{pr2}$

我尝试过HmacSHA256和HmacSHA1。似乎什么都不管用。我做错什么了?在


Tags: 字符串httpscomamazongetheredocumentationamazonaws
2条回答

我不会重新发明轮子——有几个pythonsimpledb库,比如

如果没有别的,他们的签名代码应该澄清你的错误是什么,但是说真的,使用一个现有的维护的API,它会让你的生活更轻松。在

一个关键问题是必须正确地对所有HTTP参数值进行URL编码。在

以下文档已从SimpleDB文档中消失,但可以在SQS文档中找到,并且仍然与SimpleDB非常相关:

Do not URL encode any of the unreserved characters that RFC 3986 defines.

These unreserved characters are A-Z, a-z, 0-9, hyphen ( - ), underscore ( _ ), period ( . ), and tilde ( ~ ).

Percent encode all other characters with %XY, where X and Y are hex characters 0-9 and uppercase A-F.

Percent encode extended UTF-8 characters in the form %XY%ZA

Percent encode the space character as %20 (and not +, as common encoding schemes do).

您会注意到,在Roger链接的python-simpledb模块中,它们在形成请求时遵循以下规则:

def escape(s):
  return urllib.quote(s, safe='-_~')

def urlencode(d):
  if isinstance(d, dict):
    d = d.iteritems()
  return '&'.join(['%s=%s' % (escape(k), escape(v)) for k, v in d])

相关问题 更多 >

    热门问题