- Notifications
You must be signed in to change notification settings - Fork472
JWT Tokens
Kevin Sylvestre edited this pageAug 29, 2019 ·9 revisions
Twilio Client Capability Tokens are required for setting up a device tosend and receive calls viaTwilio Client.
require'twilio-ruby'# put your own account credentials here:account_sid='ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'auth_token='yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'# set upcapability=Twilio::JWT::ClientCapability.newaccount_sid,auth_token
# Allow incoming calls, and give the client a name.incoming_scope=Twilio::JWT::ClientCapability::IncomingClientScope.new'john'capability.add_scope(incoming_scope)# Generate the token string@token=capability.to_s
# Allow outgoing calls to an application.outgoing_scope=Twilio::JWT::ClientCapability::OutgoingClientScope.new'AP11111111111111111111111111111111','john'capability.add_scope(outgoing_scope)# Generate the token string@token=capability.to_s
You can send parameters to your Application'sVoiceUrl
by passing a hash in the initialization. Here we pass along a hypothetical user id.
params={'user_id'=>@user.id}# Allow outgoing calls to an application and pass the user id to your server.outgoingScope=Twilio::JWT::ClientCapability::OutgoingClientScope.new'AP11111111111111111111111111111111','john',paramscapability.add_scope(outgoingScope)# Generate the token string@token=capability.to_s
Theuser_id
parameter and its value will be sent to your Application'sVoiceUrl
along with the other parameters that Twilio usually sends, likeFrom
,To
andCallSid
.
You can generate a Capability Token that supports multiple capabilities, so thatyour Twilio Client device can make and receive calls.
client_name='john'# Allow incoming calls, and give the client a name.incomingScope=Twilio::JWT::ClientCapability::IncomingClientScope.newclient_namecapability.add_scope(incomingScope)# Allow outgoing calls to an application and pass the user id to your server.params={'user_id'=>@user.id}outgoingScope=Twilio::JWT::ClientCapability::OutgoingClientScope.new'AP11111111111111111111111111111111',client_name,paramscapability.add_scope(outgoingScope)# Generate the token string@token=capability.to_s
By default all tokens generated with the Twilio helper libraries expire after one hour (3600s). But you should configure this expiration to be as short as possible for your application.
# Generate the token string with 5 seconds of expirationtoken=Twilio::JWT::ClientCapability.new(@account_sid,@auth_token,ttl:5)
require'twilio-ruby'# Required for any Twilio Access Tokenaccount_sid='ACxxxxxxxxxxxx'api_key='SKxxxxxxxxxxxx'api_secret='xxxxxxxxxxxxxx'# Required for Videoidentity='user'# Create Video grant for our tokenvideo_grant=Twilio::JWT::AccessToken::VideoGrant.newvideo_grant.room='cool room'# Create an Access Tokentoken=Twilio::JWT::AccessToken.new(account_sid,api_key,api_secret,[video_grant],identity:identity)# Generate the tokenputstoken.to_jwt