Ste*_*fan 3 javascript node.js twilio twilio-api
我正在开发一个使用 Twillios Programmable Video 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());
Run Code Online (Sandbox Code Playgroud)
在 Twillio 提供的这个特定示例中,明确引用了我认为是强制性的视频授权,但这意味着对于这个特定的令牌生成器,用户只能输入该名称的房间。
我想知道是否可以在配置令牌之前引用房间。类似于身份是在输出令牌之前输入到函数中的变量的方式。
另外,在 Twillios 自己的函数环境之外创建令牌时是否有任何必需的依赖项或库?
非常感谢任何答案、建议或参考。
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();
}
Run Code Online (Sandbox Code Playgroud)
然后你可以使用如下函数:
const token = generateToken("Stefan", "StefansRoom");
Run Code Online (Sandbox Code Playgroud)
让我知道这是否有帮助。