简体   繁体   English

Twilio JWT令牌已过期

[英]Twilio JWT Token expired

I am using twilio voice call and it is working properly. 我正在使用twilio语音通话,并且工作正常。 But the twilio jwt token ends every hour. 但是twilio jwt令牌每小时结束一次。 For this, the user has to refresh the page every hour. 为此,用户必须每小时刷新一次页面。 My question is how to extend the token time in nodejs. 我的问题是如何延长nodejs中的令牌时间。

I am using this code to generate a token 我正在使用此代码生成令牌

var twilio = require('twilio');
var client = twilio(config.accountSid, config.authToken);
const ClientCapability = twilio.jwt.ClientCapability;

app.get('/token', (request, response) => {   
    const capability = new ClientCapability({
        accountSid: config.accountSid,
        authToken: config.authToken
    });
    capability.addScope(
        new ClientCapability.OutgoingClientScope({
            applicationSid: config.applicationSid 
        })
    );
    const token = capability.generateToken();
    // Include token in a JSON response
    response.send({
        token: token,
    });
});

Thanks 谢谢

Twilio developer evangelist here. Twilio开发人员布道者在这里。

Access tokens do have limited lifespans. 访问令牌的寿命确实有限。 As Ankush answered, you can set a different ttl up to 24 hours. 正如Ankush回答的那样,您最多可以设置24小时的其他ttl

Better yet is to use the Twilio Access Manager . 更好的是使用Twilio Access Manager It is available as part of twilio-common for JavaScript, or the Access Manager package on iOS and Android. 它可作为twilio-common的JavaScript的一部分,或作为iOS和Android上的Access Manager软件包的一部分。

The AccessManager will fire off two events that you can hook into. AccessManager将触发您可以挂接到的两个事件。 Firstly, when a token is near expiry, it will fire a token expired event. 首先,当令牌接近到期时,它将触发令牌过期事件。 Hook into that event and when you receive it, generate a new token from your server . 挂接到该事件,并在收到事件后, 从服务器生成一个新令牌 You can then set that new token in the AccessManager. 然后,您可以在AccessManager中设置该新令牌。

Once that token is set, the AccessManager will fire a token updated event which you can listen for and update the token in your Twilio Voice Client (or Chat, Video or Sync client, depending on what you are using). 设置该令牌后,AccessManager将触发令牌更新事件,您可以在Twilio语音客户端(或Chat,Video或Sync客户端,取决于您所使用的客户端)中侦听和更新令牌。

To see how to download, install and use the Access Manager in detail for each of these platforms, take a look at the Access Token life cycle and Access Manager documentation . 要查看如何针对每个平台详细下载,安装和使用Access Manager,请查看Access令牌生命周期和Access Manager文档

Let me know if that helps at all. 让我知道是否有帮助。

From the documentation: 从文档中:

https://www.twilio.com/docs/voice/client/capability-tokens https://www.twilio.com/docs/voice/client/capability-tokens

Here, we generate a token that's only valid for ten minutes. 在这里,我们生成仅在十分钟内有效的令牌。 The expires argument expects time in seconds. expires参数期望以秒为单位的时间。

capability = ClientCapabilityToken(account_sid, auth_token, ttl=600)
print(capability.to_jwt())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM