如何在 Google Cloud Functions 中调用 Express 函数?
Posted
技术标签:
【中文标题】如何在 Google Cloud Functions 中调用 Express 函数?【英文标题】:How to call an Express function in Google Cloud Functions? 【发布时间】:2021-07-25 03:07:36 【问题描述】:这是我的 express 端点,旨在通过 webhook from Stripe 调用:
const functions = require('firebase-functions');
const bodyParser = require('body-parser')
const app = require('express')();
const stripe = require('stripe')(functions.config().stripe.test.secret);
const endpointSecret = functions.config().stripe.test.webhookkey;
app.post('/stripe_webhook', bodyParser.raw(type: 'application/json'), async (request, response) =>
const event = request.body;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret)
// Get the signature sent by Stripe
const signature = request.headers['stripe-signature'];
try
const eventConstruct = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
catch (err)
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.sendStatus(400);
console.log(`stripeEvent: $event.type`)
// Handle the event
const paymentIntent = event.data.object;
if (event.type === 'payment_intent.succeeded')
const uid = await getCustomerUID(paymentIntent.customer)
return actionRequest(uid, paymentIntent.client_secret)
// Return a 200 response to acknowledge receipt of the event
response.send();
);
我想添加此端点的 URL,以便我的 Stripe 帐户可以触发它。但是,与常规 Cloud Function 一样,它没有 URL(这是 Stripe 访问 webhook 所需要的):
有没有办法获取 URL 或以其他方式调用此端点?
PS:我正在使用 express 函数,因为下面的常规云函数失败并出现以下错误:Webhook signature verification failed. No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
代码:
exports.stripeEvent = functions.https.onRequest(async (request, response) =>
// Get the signature from the request header | Verify it's coming from Stripe
let signature = request.headers["stripe-signature"];
// Verify the request against our endpointSecret
console.log(`signature: $signature`)
console.log(`endpointSecret: $endpointSecret`)
console.log(`request.rawBody: $request.rawBody`)
try
let event = stripe.webhooks.constructEvent(request.rawBody, signature, endpointSecret)
catch (err)
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.status(400).end();
const event = request.body;
console.log(`stripeEvent: $event.type`)
// Handle the event
const paymentIntent = event.data.object;
if (event.type === 'payment_intent.succeeded')
const uid = await getCustomerUID(paymentIntent.customer)
return actionRequest(uid, paymentIntent.client_secret)
// Return a 200 response to acknowledge receipt of the event
response.json(received: true);
return 200;
);
任何建议表示赞赏。
【问题讨论】:
在部署函数时获取函数 URL。然后只需将您的快速应用程序路由附加到末尾 是的,我已经使用常规云功能(在部署后获取 URL)完成了此操作,但相同的方法不适用于如上所示的快速端点。我获取了我的基本函数 URL 并将端点添加到它:https://us-central1-<MY_APP>.cloudfunctions.net/stripe_webhook
- 但它不是从 Stripe 触发的 - 所以 URL 一定是错误的。
请分享完整代码。你在哪里定义了“http函数”?
const app = require('express')();
- 显示在第一个代码块的顶部@Dharmaraj
我的意思是HTTPS功能对不起
【参考方案1】:
const functions = require("firebase-functions");
const app = require("express")();
app.get("/test", function (req, res)
return res.sendStatus(200);
)
app.post("/stripe-event", function (req, res)
//Do the processing
return res.sendStatus(200);
)
// This is the Firebase HTTPS function
exports.api = functions.https.onRequest(app);
部署函数时,您可以在仪表板中看到该函数“api”的 URL,它应该类似于:https://<region><project-id>.cloudfunctions.net/api
。
现在,如果您想调用端点 /test
,则必须调用此 URL:"https://<region><project-id>.cloudfunctions.net/api/test"
确保你有这行:exports.api = functions.https.onRequest(app);
如果您使用它,您的函数名称将是“api”,然后将 API 端点从 express 应用程序附加到该 URL。
【讨论】:
以上是关于如何在 Google Cloud Functions 中调用 Express 函数?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Python 中运行 Google Cloud Function 中的子进程
如何修改后台 Cloud Function 的 Google Cloud Pub/Sub 订阅确认截止日期
如何从 Firebase Cloud Function 在 Google Pub/Sub 中发布消息?
如何解决 Google Cloud Function 上的“No module named 'frontend'”错误消息