【问题标题】:Generating Twillio Access Tokens using Node JS使用 Node JS 生成 Twilio 访问令牌
【发布时间】:2017-07-26 04:19:13
【问题描述】:

我正在开发一个使用 Twillios 可编程视频 API 的应用程序。

我是使用 Node JS 的新手,但文档相当简单,但我仍有一些问题。

这是我引用的代码。

const AccessToken = require('twilio').jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;

// Used when generating any kind of tokens
const twilioAccountSid = 'ACxxxxxxxxxx';
const twilioApiKey = 'SKxxxxxxxxxx';
const twilioApiSecret = 'xxxxxxxxxxxx';

const identity = 'user';

// Create Video Grant
const videoGrant = new VideoGrant({
room: 'cool room'
});

// Create an access token which we will sign and return to the client,
// containing the grant we just created
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret);
token.addGrant(videoGrant);
token.identity = identity;

// Serialize the token to a JWT string
console.log(token.toJwt());

在 Twillio 提供的这个特定示例中,明确引用了我认为是强制性的视频授权,但这意味着对于这个特定的令牌生成器,用户只能输入该名称的房间。

我想知道是否可以在配置令牌之前引用房间。类似于身份是在输出令牌之前输入到函数中的变量的方式。

另外,在 Twillios 自己的函数环境之外创建令牌时,是否有任何必需的依赖项或库?

非常感谢任何答案、建议或参考。

【问题讨论】:

  • 你的意思是什么,你想将房间存储在其他变量中以供以后引用吗?你能说得清楚一点吗。

标签: javascript node.js twilio twilio-api


【解决方案1】:

这里是 Twilio 开发者宣传员。

也可以将房间名称作为变量提供。您可能想要创建一个函数,该函数可以将身份和房间名称作为参数并返回访问令牌。像这样的:

const AccessToken = require('twilio').jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;

// Used when generating any kind of tokens
const twilioAccountSid = 'ACxxxxxxxxxx';
const twilioApiKey = 'SKxxxxxxxxxx';
const twilioApiSecret = 'xxxxxxxxxxxx';


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();
}

然后你可以使用如下函数:

const token = generateToken("Stefan", "StefansRoom");

让我知道这是否有帮助。

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 2017-06-22
    • 2020-08-10
    • 1970-01-01
    相关资源
    最近更新 更多