我正在使用移动API https://dev.moves-app.com获取一些健身数据。我不想定期查询 API,而是使用故事情节通知 https://dev.moves-app.com/docs/api_notifications.
它有效,我收到来自 API 的请求,但无法验证请求中提供的 hmac sha1 签名。
文档说:
所有通知请求均使用 Base64 编码的 HMAC-SHA1 进行签名
签名。签名的计算方式为 HMAC_SHA1(,||),换句话说就是客户端
Secret 作为密钥和请求正文、时间戳和随机数连接
作为消息数据。 HTTP 标头不包含在签名中。
标头 X-Moves-Signature、X-Moves-Timestamp 和 X-Moves-Nonce
包含签名、时间戳和随机数值。时间戳是一个
unix 时间戳,自 1970 年 1 月 1 日 00:00:00 GMT 起的秒数。
我的实施:
from hmac import new as hmac_new
from hashlib import sha1
def check_signature(signature, timestamp, nonce, client_secret, request_body):
msg = request_body + timestamp.encode('utf-8') + nonce.encode('utf-8')
hmac = hmac_new(key=client_secret, msg=msg, digestmod=sha1)
return hmac.digest().encode('base64') == signature
我收到来自 Flask 的请求并像这样调用我的函数:
check_signature(headers['X-Moves-Signature'], headers['X-Moves-Timestamp'], headers['X-Moves-Nonce'], settings['client-secret'], request.data)
Values:
client-secret= mnMuu6rDMkeG5FL0Fm0ho2z14JUhMVWAntUnGz0VyXc446RtqP8J7ETfag0TQa58
request-body = {"userId": 34511428141091768, "storylineUpdates": [{"reason": "DataUpload", "endTime": "20150429T121602Z", "lastSegmentType": "place", "lastSegmentStartTime": "20150429T101434Z", "startTime": "20150429T101434Z"}]}
X-Moves-Nonce = eqVCO4bnNbN+8Hhiz7ZceA==
X-Moves-Signature = BRMwYCxglul01wbyXpfpdtiJh2Y=
X-Moves-Timestamp = 1430309780
my-digest = paWR/3yiJ8NT8KukorGVJlpmQeM=
my-hexdigest = a5a591ff7ca227c353f0aba4a2b195265a6641e3
moves_signature = BRMwYCxglul01wbyXpfpdtiJh2Y=
我也尝试过http://www.freeformatter.com/hmac-generator.html http://www.freeformatter.com/hmac-generator.html并且还收到了a5a591ff7ca227c353f0aba4a2b195265a6641e3
.
(客户端密钥不再有效)。
正如您从值中看到的那样,我的摘要和 move_signature 不相等。遗憾的是,我无法获得与移动中的摘要相同的摘要,但我无法找到问题所在。有人知道如何解决这个问题吗?