正如Twitter Docs中提到的验证请求的步骤
这是我的代码:
const buffer = Buffer.from(JSON.stringify(ctx.request.body))
const expectedHash = crypto.createHmac('sha256', TWITTER_CONSUMER_SECRET).update(buffer).digest('base64')
来自twitter的x-twitter-webhooks-signature
标头和生成的哈希值不匹配。此外,https://twittercommunity.com/t/validating-the-webhook-signature-header-in-node-js/102525的帖子说它是JSON.stringify()
问题。我正在使用koa和koa-body,如何修复哈希不匹配?
找到了解决方案。使用koa-bodyparser
而不是koa-body
然后sha256
的ctx.request.rawBody
与标题匹配。