gpt4 book ai didi

javascript - HMAC - 通过 Postman 进行 SHA256 身份验证

转载 作者:行者123 更新时间:2023-12-05 03:40:03 27 4
gpt4 key购买 nike

我正在尝试通过 Postman 模拟对我的 Rails 应用程序的 webhook POST 请求(在实际工作流程中运行良好)。我找到了很多示例,但没有一个有效 - 我一直收到 401 代码。我所做的是定义 header 和 Pre-request Script,如下所示:

postman headers

JS 作为 Pre-request Script 基于 this docs

postman.setEnvironmentVariable("hmac", CryptoJS.HmacSHA256(request.data, 'my_secret_string').toString(CryptoJS.digest));

我仍然遇到 401 错误。

我用来触发 webhook 的外部 API 文档明确指出:

Each webhook will be sent with the​ X-AQID-Signature​ header, which is created by hashing the request's payload with the HMAC method and SHA256 algorithm, using the shared secret as salt. This means that upon receiving a payload, you can verify its integrity by replicating the hashing method.

就像我说的那样,它在现实生活中的工作流程中运行良好,所以我在 postman 实现中遇到了错误。我错过了什么?

最佳答案

您不需要设置任何环境变量,只需从脚本中添加一个 header 即可。我在一个非常相似的案例中这样做过:

var signBytes = CryptoJS.HmacSHA256(pm.request.body.raw, 'YOUR_SECRET');
var signHex = CryptoJS.enc.Hex.stringify(signBytes);
pm.request.headers.add({
key: "HEADER_NAME",
value: signHex
});

关于javascript - HMAC - 通过 Postman 进行 SHA256 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68305228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com