【发布时间】:2020-03-26 19:57:07
【问题描述】:
编辑:
comment by CBroe 是解决方案。确保在进行哈希之前,来自 Facebook 的负载未解析。
我正在尝试使用 the steps in the Facebook developer docs 验证来自 Instagram Graph API 的 webhook 有效负载的内容。
尽管遵循了文档中的所有步骤,但我似乎无法生成匹配的哈希。需要明确的是,我的代码成功创建了一个哈希,它与 Facebook 通过 webhook 发送的不匹配。
我的代码如下(应用秘密和有效负载内容使用了虚拟值)。任何帮助将不胜感激。
const crypto = require('crypto')
// Escape special characters
const escapeUnicode = str => {
return str.replace(/[\u00A0-\uffff]/gu, function (c) {
return "\\u" + ("000" + c.charCodeAt().toString(16)).slice(-4)
})
}
// Dummy values
const payload = {
"object": "instagram",
"entry": [
{
"id": "some-id",
"time": 1234567890,
"changes": [
{
"value": {
"media_id": "some-id",
"impressions": 0,
"reach": 0,
"taps_forward": 0,
"taps_back": 0,
"exits": 0,
"replies": 0
},
"field": "story_insights"
}
]
}
]
}
// Create hash
const hmac = crypto.createHmac('sha1', 'my-app-secret')
hmac.update(escapeUnicode(JSON.stringify(payload)))
const hash = `sha1=${hmac.digest('hex')}`
【问题讨论】:
-
您是否将 Facebook 发送的 original 有效负载作为输入?如果您先解码数据,然后再次使用
JSON.stringify,则格式可能可能会有所不同,当涉及到空格/JSON“格式化”时。 -
这是解决方案,谢谢!
标签: node.js facebook validation instagram facebook-webhooks