Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit a994a24

Browse files
committed
Added Sync Token Examples
1 parent 5687153 commit a994a24

File tree

7 files changed

+204
-0
lines changed

7 files changed

+204
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "Creating an Access Token (Sync)",
3+
"type": "server"
4+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const AccessToken = require('twilio').jwt.AccessToken;
2+
const SyncGrant = AccessToken.SyncGrant;
3+
4+
// Used when generating any kind of tokens
5+
// To set up environmental variables, see http://twil.io/secure
6+
const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID;
7+
const twilioApiKey = process.env.TWILIO_API_KEY;
8+
const twilioApiSecret = process.env.TWILIO_API_SECRET;
9+
10+
// Used specifically for creating Sync tokens
11+
const outgoingApplicationSid = 'APxxxxxxxxxxxxx';
12+
const identity = 'user';
13+
14+
// Create a "grant" which enables a client to use Sync as a given user
15+
const syncGrant = new SyncGrant({
16+
outgoingApplicationSid: outgoingApplicationSid,
17+
incomingAllow: true, // Optional: add to allow incoming calls
18+
});
19+
20+
// Create an access token which we will sign and return to the client,
21+
// containing the grant we just created
22+
const token = new AccessToken(
23+
twilioAccountSid,
24+
twilioApiKey,
25+
twilioApiSecret,
26+
{ identity: identity }
27+
);
28+
token.addGrant(syncGrant);
29+
30+
// Serialize the token to a JWT string
31+
console.log(token.toJwt());
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Twilio.Jwt.AccessToken;
4+
5+
class Example
6+
{
7+
static void Main(string[] args)
8+
{
9+
// These values are necessary for any access token
10+
// To set up environmental variables, see http://twil.io/secure
11+
const string twilioAccountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
12+
const string twilioApiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");
13+
const string twilioApiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");
14+
15+
// These are specific to Sync
16+
const string outgoingApplicationSid = "APXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
17+
const string identity = "user";
18+
19+
// Create a Sync grant for this token
20+
var grant = new SyncGrant();
21+
grant.OutgoingApplicationSid = outgoingApplicationSid;
22+
23+
// Optional: add to allow incoming calls
24+
grant.IncomingAllow = true;
25+
26+
var grants = new HashSet<IGrant>
27+
{
28+
{ grant }
29+
};
30+
31+
// Create an Access Token generator
32+
var token = new Token(
33+
twilioAccountSid,
34+
twilioApiKey,
35+
twilioApiSecret,
36+
identity,
37+
grants: grants);
38+
39+
Console.WriteLine(token.ToJwt());
40+
}
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
// Get the PHP helper library from https://twilio.com/docs/libraries/php
3+
require_once '/path/to/vendor/autoload.php'; // Loads the library
4+
use Twilio\Jwt\AccessToken;
5+
use Twilio\Jwt\Grants\SyncGrant;
6+
7+
// Required for all Twilio access tokens
8+
// To set up environmental variables, see http://twil.io/secure
9+
$twilioAccountSid = getenv('TWILIO_ACCOUNT_SID');
10+
$twilioApiKey = getenv('TWILIO_API_KEY');
11+
$twilioApiSecret = getenv('TWILIO_API_KEY_SECRET');
12+
13+
// Required for Sync grant
14+
$outgoingApplicationSid = 'APxxxxxxxxxxxx';
15+
// An identifier for your app - can be anything you'd like
16+
$identity = "john_doe";
17+
18+
// Create access token, which we will serialize and send to the client
19+
$token = new AccessToken(
20+
$twilioAccountSid,
21+
$twilioApiKey,
22+
$twilioApiSecret,
23+
3600,
24+
$identity
25+
);
26+
27+
// Create Sync grant
28+
$syncGrant = new SyncGrant();
29+
$syncGrant->setOutgoingApplicationSid($outgoingApplicationSid);
30+
31+
// Optional: add to allow incoming calls
32+
$syncGrant->setIncomingAllow(true);
33+
34+
// Add grant to token
35+
$token->addGrant($syncGrant);
36+
37+
// render token to string
38+
echo $token->toJWT();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'twilio-ruby'
2+
3+
# Required for any Twilio Access Token
4+
# To set up environmental variables, see http://twil.io/secure
5+
account_sid = ENV['TWILIO_ACCOUNT_SID']
6+
api_key = ENV['TWILIO_API_KEY']
7+
api_secret = ENV['TWILIO_API_KEY_SECRET']
8+
9+
# Required for Sync
10+
outgoing_application_sid = 'APxxxxxxxxxxxx'
11+
identity = 'user'
12+
13+
# Create Sync grant for our token
14+
grant = Twilio::JWT::AccessToken::SyncGrant.new
15+
grant.outgoing_application_sid = outgoing_application_sid
16+
17+
# Optional: add to allow incoming calls
18+
grant.incoming_allow = true
19+
20+
# Create an Access Token
21+
token = Twilio::JWT::AccessToken.new(
22+
account_sid,
23+
api_key,
24+
api_secret,
25+
[grant],
26+
identity: identity
27+
)
28+
29+
# Generate the token
30+
puts token.to_jwt
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
from twilio.jwt.access_token import AccessToken
3+
from twilio.jwt.access_token.grants import SyncGrant
4+
5+
# required for all twilio access tokens
6+
# To set up environmental variables, see http://twil.io/secure
7+
account_sid = os.environ['TWILIO_ACCOUNT_SID']
8+
api_key = os.environ['TWILIO_API_KEY']
9+
api_secret = os.environ['TWILIO_API_KEY_SECRET']
10+
11+
# required for Sync grant
12+
outgoing_application_sid = 'APxxxxxxxxxxxxx'
13+
identity = 'user'
14+
15+
# Create access token with credentials
16+
token = AccessToken(account_sid, api_key, api_secret, identity=identity)
17+
18+
# Create a Sync grant and add to token
19+
sync_grant = SyncGrant(
20+
outgoing_application_sid=outgoing_application_sid,
21+
incoming_allow=True, # Optional: add to allow incoming calls
22+
)
23+
token.add_grant(sync_grant)
24+
25+
# Return token info as JSON
26+
print(token.to_jwt())
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import com.twilio.jwt.accesstoken.AccessToken;
2+
import com.twilio.jwt.accesstoken.SyncGrant;
3+
4+
public class TokenGenerator {
5+
6+
public static void main(String[] args) {
7+
// Get your Account SID from https://twilio.com/console
8+
// To set up environment variables, see http://twil.io/secure
9+
// Required for all types of tokens
10+
String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID");
11+
String twilioApiKey = System.getenv("TWILIO_API_KEY");
12+
String twilioApiSecret = System.getenv("TWILIO_API_SECRET");
13+
14+
// Required for Sync
15+
String outgoingApplicationSid = System.getenv("TWILIO_APP_SID");
16+
String identity = "user";
17+
18+
// Create Sync grant
19+
SyncGrant grant = new SyncGrant();
20+
grant.setOutgoingApplicationSid(outgoingApplicationSid);
21+
22+
// Optional: add to allow incoming calls
23+
grant.setIncomingAllow(true);
24+
25+
// Create access token
26+
AccessToken token = new AccessToken.Builder(
27+
twilioAccountSid,
28+
twilioApiKey,
29+
twilioApiSecret
30+
).identity(identity).grant(grant).build();
31+
32+
System.out.println(token.toJwt());
33+
}
34+
}

0 commit comments

Comments
 (0)