【发布时间】:2013-12-31 00:21:54
【问题描述】:
我在使用 HMAC 时遇到了一点问题。运行这段代码时:
signature = hmac.new(
key=secret_key,
msg=string_to_sign,
digestmod=sha1,
)
我收到一个奇怪的错误:
File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hmac.py", line 133, in new
return HMAC(key, msg, digestmod)
File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hmac.py", line 72, in __init__
self.outer.update(key.translate(trans_5C))
TypeError: character mapping must return integer, None or unicode
当我打印 string_to_sign 时,它是一个像这样的正确字符串:
GET
\n
\n
application/json
\n
\n
\n
错误是什么意思?是因为换行吗?
【问题讨论】:
-
看看here.
-
@Faust 谢谢。您可以将其发布为答案吗?我使用 django.utils.encoding.smart_bytes 将 unicode 转换为字节字符串,然后它就可以工作了。
标签: python