javascript - 出现错误:测试 webhook 错误:尝试将测试事件发送到 webhook 端点时出现 400
问题描述
我正在尝试按照本教程中的说明发送测试 webhook。
但是当我去做时,我在第一个链接中看到了错误,下面是:
测试 webhook 错误:400
这是我部署到 firebase 函数的 index.ts 代码和函数。
import * as functions from 'firebase-functions';
// const functions = require('firebase-functions');
const stripe = require('stripe')(functions.config().keys.webhooks);
const admin = require('firebase-admin');
admin.initializeApp();
const endpointSecret = functions.config().keys.signing;
exports.events = functions.https.onRequest((request, response) => {
let sig = request.headers["stripe-signature"];
try {
let event = stripe.webhooks.constructEvent(request.rawBody, sig, endpointSecret); // Validate the request
return admin.database().ref('/events').push(event) // Add the event to the database
.then((snapshot: { ref: { toString: () => any; }; }) => {
// Return a successful response to acknowledge the event was processed successfully
return response.json({ received: true, ref: snapshot.ref.toString() });
})
.catch((err: any) => {
console.error(err) // Catch any errors saving to the database
return response.status(500).end();
});
}
catch (err) {
return response.status(400).end(); // Signing signature failure, return an error 400
}
});
exports.exampleDatabaseTrigger = functions.database.ref('/events/{eventId}').onCreate((snapshot, context) => {
return console.log({
eventId: context.params.eventId,
data: snapshot.val()
});
});
如何解决此问题并成功运行测试?
我目前的想法是这个问题可能与:
我是如何写下这一行的:snapshot: { ref: { toString: () => any; };
更新:
从我的测试来看,情况似乎并非如此。
解决方案
我不相信“测试网络钩子”正确地签署了它们;您应该为此使用Stripe CLI 。
推荐阅读
- c# - 删除字符串中的双反斜杠
- ios - 无法将“NSManagedObject”类型的返回表达式转换为“字符串”类型?
- sql - 使用 javalite 找不到表异常
- c# - 为什么我的页面总是重定向到不存在的actionmethod视图而不是返回到调用actionmethod的视图
- python - 使用 tflite 为 android 转换 Mask-RCNN 模型
- html - 如何在 XSLT1.0 的中间位置使用 xsl:template?
- android - 生成的绑定类仅将 LayoutInflater 作为其唯一参数
- java - Linux arm64上的java错误下载x86库
- python - Cartopy 和 ECMWF 数据
- java - 如何在测试自动化中处理 Firefox 浏览器确认消息?