javascript - Generating Twillio Access Tokens using Node JS -


i'm developing application uses twillios programmable video api.

i'm new using node js, documentation has been straightforward, still have few questions.

here's code referencing.

const accesstoken = require('twilio').jwt.accesstoken; const videogrant = accesstoken.videogrant;  // used when generating 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 access token sign , return client, // containing grant created const token = new accesstoken(twilioaccountsid, twilioapikey, twilioapisecret); token.addgrant(videogrant); token.identity = identity;  // serialize token jwt string console.log(token.tojwt()); 

in specific example provided twillio, video grant assume mandatory explicitly referenced, mean specific token generator, users can enter rooms of name.

i wondering if possible reference room before configuring token. similar how identity variable that's entered function before token output.

in addition, there required dependencies or libraries when creating tokens outside of twillios own function environment?

any answers, suggestions, or references appreciated.

twilio developer evangelist here.

it possible supply room name variable too. might want create function can take identity , room name arguments , returns access token. this:

const accesstoken = require('twilio').jwt.accesstoken; const videogrant = accesstoken.videogrant;  // used when generating 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(); } 

then can use function like:

const token = generatetoken("stefan", "stefansroom"); 

let me know if helps @ all.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -