【问题标题】:how to use firebase https trigger functions如何使用firebase https触发功能
【发布时间】:2017-07-27 09:10:27
【问题描述】:

我正在使用 firebase 云函数创建令牌生成器,并且我想使用 https 触发器来创建令牌,但是我需要在对 url 的调用中包含数据。我知道这是可能的,但我不一定知道该怎么做。

我需要这个,所以我可以在我的函数中为某些变量设置值。

所以最终的 url 在伪代码中可能看起来像这样:

https://tokengen/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 时,它返回 Error: could not handle the request

【问题讨论】:

标签: node.js firebase google-cloud-functions


【解决方案1】:

你可以这样做 -

https://yourFB.cloudfunctions.net/token?identity=12&room=12

你可以像这样使用它-

exports.token = functions.https.onRequest((request, response) => {
    const { identity, room } = request.query;
    ...
});

希望这会有所帮助。

【讨论】:

  • 嗨,我还是有点麻烦。我收到错误无法处理请求。您介意查看代码吗?我认为这与处理响应有关。我将在问题中包含代码作为编辑。
  • 嘿,抱歉耽搁了,你能给我看看你完整的url吗,另一件要注意的是你正在使用twilio,这是外部服务,你必须使用付费的firebase帐户
  • 您所说的“使用付费 Firebase 帐户”是什么意思,您是否需要付费帐户才能使用云功能?这里还有网址us-central1-videochattest-dbd0d.cloudfunctions.net/…
  • 你能去logs看看它在打印什么吗?如果您使用 google api,那么它是免费的。如果您使用外部 api,那么您必须使用付费帐户。我们要不要先检查日志并确认是否是这种情况
  • 哦,好吧,我在日志中看到了错误。就是这么说的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 2020-10-20
相关资源
最近更新 更多