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

Commit 3ea5543

Browse files
authored
Merge pull request #937 from deshartman/sync_examples
Sync examples
2 parents ec2207b + 1325f94 commit 3ea5543

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
const twilioSyncService = process.env.TWILIO_SYNC_SERVICE_SID;
10+
11+
// Used specifically for creating Sync tokens
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+
serviceSid: twilioSyncService,
17+
});
18+
19+
// Create an access token which we will sign and return to the client,
20+
// containing the grant we just created
21+
const token = new AccessToken(
22+
twilioAccountSid,
23+
twilioApiKey,
24+
twilioApiSecret,
25+
{ identity: identity }
26+
);
27+
token.addGrant(syncGrant);
28+
29+
// Serialize the token to a JWT string
30+
console.log(token.toJwt());
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
const string twilioSyncService = Environment.GetEnvironmentVariable("TWILIO_SYNC_SERVICE_SID");
15+
16+
// These are specific to Sync
17+
const string identity = "user";
18+
19+
// Create a Sync grant for this token
20+
var grant = new SyncGrant();
21+
22+
// Create a "grant" which enables a client to use Sync as a given user
23+
grant.serviceSid = twilioSyncService;
24+
25+
var grants = new HashSet<IGrant>
26+
{
27+
{ grant }
28+
};
29+
30+
// Create an Access Token generator
31+
var token = new Token(
32+
twilioAccountSid,
33+
twilioApiKey,
34+
twilioApiSecret,
35+
identity,
36+
grants: grants);
37+
38+
Console.WriteLine(token.ToJwt());
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
$twilioSyncService = getenv("TWILIO_SYNC_SERVICE_SID");
13+
14+
15+
// Required for Sync grant
16+
// An identifier for your app - can be anything you'd like
17+
$identity = "john_doe";
18+
19+
// Create access token, which we will serialize and send to the client
20+
$token = new AccessToken(
21+
$twilioAccountSid,
22+
$twilioApiKey,
23+
$twilioApiSecret,
24+
3600,
25+
$identity
26+
);
27+
28+
// Create Sync grant
29+
$syncGrant = new SyncGrant();
30+
31+
// Create a "grant" which enables a client to use Sync as a given user
32+
$syncGrant->setServiceSid($twilioSyncService);
33+
34+
35+
// Add grant to token
36+
$token->addGrant($syncGrant);
37+
38+
// render token to string
39+
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+
twilio_sync_service = ENV['TWILIO_SYNC_SERVICE_SID']
9+
10+
11+
# Required for Sync
12+
identity = 'user'
13+
14+
# Create Sync grant for our token
15+
grant = Twilio::JWT::AccessToken::SyncGrant.new
16+
17+
# Create a "grant" which enables a client to use Sync as a given user
18+
grant.service_sid = twilio_sync_service
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
twilio_sync_service = os.environ[process.env.['TWILIO_SYNC_SERVICE_SID']
11+
12+
# required for Sync grant
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+
# Create a "grant" which enables a client to use Sync as a given user
21+
service_sid=twilio_sync_service
22+
)
23+
24+
token.add_grant(sync_grant)
25+
26+
# Return token info as JSON
27+
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+
String twilioSyncService = System.getenv("TWILIO_SYNC_SERVICE_SID");
14+
15+
16+
// Required for Sync
17+
String identity = "user";
18+
19+
// Create Sync grant
20+
SyncGrant grant = new SyncGrant();
21+
22+
// Create a "grant" which enables a client to use Sync as a given user
23+
grant.setServiceSid(twilioSyncService);
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)