【发布时间】:2023-03-11 08:20:01
【问题描述】:
所以我在这里有点困惑。
我正在使用一个将 webhook 发送到我的服务器的 api。它允许我们验证请求是否来自 api。
我正在使用 django rest 框架。
签名是这样过来的
Mux-Signature: t=1565220904,v1=20c75c1180c701ee8a796e81507cfd5c932fc17cf63a4a55566fd38da3a2d3d2
t= 之后的值是时间戳。 v1=之后的值就是签名
muxsecurityheader = request.META['HTTP_MUX_SIGNATURE']
spiltmuxsecurityheader = muxsecurityheader.split(',')
timestamp = spiltmuxsecurityheader[0]
timestamp = timestamp[2:]
receviedsignature = spiltmuxsecurityheader[1]
receviedsignature = receviedsignature[3:]
secretkey = 'a string that is my key'
payload = timestamp + '.' + str(request.body)
expectedsignature = hmac.new(secretkey, bytes(payload, 'UTF-8'), hashlib.sha256).hexdigest()
print(expectedsignature)
是我目前的代码。
在 apis 文档 https://docs-legacy.mux.com/docs/webhook-security 的示例中
它声明通过提供的示例确定预期的签名。显然这只是伪代码
secret = // your signing secret
payload = timestamp + "." + request_body
expected_signature = createHmacSha256(payload, secret)
这与我上面的代码相似。
但是我遇到了一些错误并且
expectedsignature = hmac.new(secretkey, bytes(payload, 'UTF-8'), hashlib.sha256).hexdigest()
raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str'
就是其中之一。
例如,我正在尝试解决一个问题。而且我正处于你们中的一个人将立即看到我做错了什么的地步。非常感谢您提供一些清晰的说明。
【问题讨论】: