3.x与2.x

时间:2016-11-17 03:45:16

标签: python-3.x signature amazon-mws

我正在处理在Python 2.7中运行的代码,但是当我在3.6中使用它时,它不起作用。它的意思是为亚马逊MWS API调用编码签名。

Python 2.7中的原始代码:

sig_encoded = base64.b64encode(hmac.new(str(self.secret_key), sig_data, hashlib.sha256).digest())

我在这里阅读了其他一些帖子,按照说明操作,并提出了这个:

Python 3.6

    key_enc = (bytes(self.secret_key, "utf-8"))
    sig_data_enc = (bytes(sig_data, "utf-8"))
    sig_encoded =  base64.b64encode(hmac.new(key_enc, sig_data_enc, hashlib.sha256).digest())

但是,这会从API返回错误。 Python 3.6中使用的版本有什么问题?

谢谢!

1 个答案:

答案 0 :(得分:2)

我能找到答案。在Python 3.6中运行的代码是:

return base64.b64encode(
        hmac.new(str(self.secret_key).encode('utf-8'), sig_data.encode('utf-8'), hashlib.sha256).digest())