Validating payload from webhook
1

Hi, I'm trying to validate the contents of a payload from Instagram using the steps here - https://developers.facebook.com/docs/graph-api/webhooks/getting-started#validate-payloads

I can't seem to generate a matching hash, despite following all the steps. My code is below (with dummy values used for app secret and payload contents). Any help would be greatly appreciated.

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')}`
Lewis
Asked about 4 months ago