Reputation: 532
I'm using Twilio's Java helper library to generate a capability token for my Angular app to use to make calls from the browser. Here's the code:
public class CapabilityToken {
private TwilioCapability capability;
public String get(String applicationSid) {
capability.allowClientOutgoing(applicationSid);
try {
String token = capability.generateToken();
// logging happens here
return token;
} catch (DomainException e) {
e.printStackTrace();
}
}
}
On the Angular side, we make a call to our API to fetch a new token and then use that to initialize the device.
Twilio.Device.setup(token, {debug: true});
I noticed in the logs that the token gets longer and longer as repeated calls are made to generate this token.
The documentation recommends generating a token every time a new phone call is made, but when I tried that, the token quickly became unusable because it was too long. So now I'm just fetching the token on page load, but the token still gets really long too quickly.
Why is this happening and where could I be going wrong?
Upvotes: 1
Views: 187
Reputation: 73055
Twilio developer evangelist here.
The way you have set your CapabilityToken
class up is the issue here. You seem to be storing a capability token as a class level variable and every time you call get
on the class you add another outgoing client permission to it. That's why it is increasing in size each time. Instead, you should generate a new TwilioCapability
each time, like so:
public class CapabilityToken {
public String get(String applicationSid) {
TwilioCapability capability = new TwilioCapability(ACCOUNT_SID, AUTH_TOKEN);
capability.allowClientOutgoing(applicationSid);
try {
String token = capability.generateToken();
// logging happens here
return token;
} catch (DomainException e) {
e.printStackTrace();
}
}
}
Let me know if that helps.
Upvotes: 1