如何使用 firebase https 触发函数
how to use firebase https trigger functions
我正在使用 firebase 云函数创建令牌生成器,我想使用 https 触发器来创建令牌,但是我需要在对 url 的调用中包含数据。我知道这是可能的,但我不一定知道该怎么做。
我需要这个,这样我就可以在我的函数中为某些变量设置值。
所以最终的 url 在伪代码中可能看起来像这样:
https://tokengen/identity=/room=
这里,identity 和 room 是我想在调用函数时为变量包含的两个值。
重申一下,
我知道您可以使用以下方式请求数据:
exports.token = functions.https.onRequest((request, response) => {
但我如何将数据与 https 调用一起包含为变量。一个例子将不胜感激。与任何答案、建议或参考一样。
编辑:
这是更新后的代码,
exports.tokenGenerator = functions.https.onRequest((request, response) => {
const { identity, roomName } = request.query;
const AccessToken = require('twilio').jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
const twilioAccountSid = '1xxxxxxxxxx';
const twilioApiKey = '1xxxxxxxxxx';
const twilioApiSecret = '1xxxxxxxxxx';
function generateToken(identity, roomName) {
const videoGrant = new VideoGrant({
room: roomName
});
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret);
token.addGrant(videoGrant);
token.identity = identity;
return token.toJwt();
}
response.send(token.toJwt());
});
当我使用url它returnsError: could not handle the request
你可以这样做 -
https://yourFB.cloudfunctions.net/token?identity=12&room=12
你可以像-
一样使用它
exports.token = functions.https.onRequest((request, response) => {
const { identity, room } = request.query;
...
});
希望对您有所帮助。
我正在使用 firebase 云函数创建令牌生成器,我想使用 https 触发器来创建令牌,但是我需要在对 url 的调用中包含数据。我知道这是可能的,但我不一定知道该怎么做。
我需要这个,这样我就可以在我的函数中为某些变量设置值。
所以最终的 url 在伪代码中可能看起来像这样:
https://tokengen/identity=/room=
这里,identity 和 room 是我想在调用函数时为变量包含的两个值。
重申一下,
我知道您可以使用以下方式请求数据:
exports.token = functions.https.onRequest((request, response) => {
但我如何将数据与 https 调用一起包含为变量。一个例子将不胜感激。与任何答案、建议或参考一样。
编辑:
这是更新后的代码,
exports.tokenGenerator = functions.https.onRequest((request, response) => {
const { identity, roomName } = request.query;
const AccessToken = require('twilio').jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
const twilioAccountSid = '1xxxxxxxxxx';
const twilioApiKey = '1xxxxxxxxxx';
const twilioApiSecret = '1xxxxxxxxxx';
function generateToken(identity, roomName) {
const videoGrant = new VideoGrant({
room: roomName
});
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret);
token.addGrant(videoGrant);
token.identity = identity;
return token.toJwt();
}
response.send(token.toJwt());
});
当我使用url它returnsError: could not handle the request
你可以这样做 -
https://yourFB.cloudfunctions.net/token?identity=12&room=12
你可以像-
一样使用它exports.token = functions.https.onRequest((request, response) => {
const { identity, room } = request.query;
...
});
希望对您有所帮助。