使用节点JS生成Twillio访问令牌

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

我刚刚使用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提供的这个特定的例子中,我认为这个video授权是强制性的,但是对于这个特定的令牌生成器,用户只能input这个名字的房间。

我想知道configuration令牌之前是否可以引用这个房间。 类似于身份是如何在输出令牌之前input到函数中的variables。

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

任何答案,build议或参考非常感谢。

Twilio开发者在这里传道。

也可以提供房间名称作为variables。 您可能需要创build一个可以将身份和房间名称作为参数的函数,并返回一个访问令牌。 像这样的东西:

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

那么你可以使用像这样的function:

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

让我知道这是否有帮助。