Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用节点JS生成Twillio访问令牌_Javascript_Node.js_Twilio_Twilio Api - Fatal编程技术网

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

Javascript 使用节点JS生成Twillio访问令牌,javascript,node.js,twilio,twilio-api,Javascript,Node.js,Twilio,Twilio Api,我正在开发一个使用Twillios可编程视频API的应用程序 我不熟悉使用Node JS,但是文档已经相当简单,但是我仍然有一些问题 这是我引用的代码 const AccessToken = require('twilio').jwt.AccessToken; const VideoGrant = AccessToken.VideoGrant; // Used when generating any kind of tokens const twilioAccountSid = 'ACxxxx

我正在开发一个使用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自己的函数环境之外创建令牌时,是否有任何必需的依赖项或库


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

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");

让我知道这是否有帮助。

您的观点是什么,是否要将房间存储在其他变量中以供以后参考?你能再清楚一点吗,伙计。