2017-07-26 110 views
1

我正在開發一個使用Twillios可編程視頻API的應用程序。使用節點JS生成Twillio訪問令牌

我剛剛使用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自己的功能環境之外創建令牌時是否還有任何所需的依賴項或庫?

任何答案,建議或參考非常感謝。

+0

您的觀點是什麼?您想將房間存儲在其他變量中以供日後參考嗎?你能更清楚的配偶嗎? –

回答

3

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

讓我知道這是否有助於在所有。