Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rest/access-tokens/sync-example/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Creating an Access Token (Sync)",
"type": "server"
}
30 changes: 30 additions & 0 deletions rest/access-tokens/sync-example/sync-example.3.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const AccessToken = require('twilio').jwt.AccessToken;
const SyncGrant = AccessToken.SyncGrant;

// Used when generating any kind of tokens
// To set up environmental variables, see http://twil.io/secure
const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID;
const twilioApiKey = process.env.TWILIO_API_KEY;
const twilioApiSecret = process.env.TWILIO_API_SECRET;
const twilioSyncService = process.env.TWILIO_SYNC_SERVICE_SID;

// Used specifically for creating Sync tokens
const identity = 'user';

// Create a "grant" which enables a client to use Sync as a given user
const syncGrant = new SyncGrant({
serviceSid: twilioSyncService,
});

// Create an access token which we will sign and return to the client,
// containing the grant we just created
const token = new AccessToken(
twilioAccountSid,
twilioApiKey,
twilioApiSecret,
{ identity: identity }
);
token.addGrant(syncGrant);

// Serialize the token to a JWT string
console.log(token.toJwt());
40 changes: 40 additions & 0 deletions rest/access-tokens/sync-example/sync-example.5.x.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using Twilio.Jwt.AccessToken;

class Example
{
static void Main(string[] args)
{
// These values are necessary for any access token
// To set up environmental variables, see http://twil.io/secure
const string twilioAccountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
const string twilioApiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");
const string twilioApiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");
const string twilioSyncService = Environment.GetEnvironmentVariable("TWILIO_SYNC_SERVICE_SID");

// These are specific to Sync
const string identity = "user";

// Create a Sync grant for this token
var grant = new SyncGrant();

// Create a "grant" which enables a client to use Sync as a given user
grant.serviceSid = twilioSyncService;

var grants = new HashSet<IGrant>
{
{ grant }
};

// Create an Access Token generator
var token = new Token(
twilioAccountSid,
twilioApiKey,
twilioApiSecret,
identity,
grants: grants);

Console.WriteLine(token.ToJwt());
}
}
39 changes: 39 additions & 0 deletions rest/access-tokens/sync-example/sync-example.5.x.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\SyncGrant;

// Required for all Twilio access tokens
// To set up environmental variables, see http://twil.io/secure
$twilioAccountSid = getenv('TWILIO_ACCOUNT_SID');
$twilioApiKey = getenv('TWILIO_API_KEY');
$twilioApiSecret = getenv('TWILIO_API_KEY_SECRET');
$twilioSyncService = getenv("TWILIO_SYNC_SERVICE_SID");


// Required for Sync grant
// An identifier for your app - can be anything you'd like
$identity = "john_doe";

// Create access token, which we will serialize and send to the client
$token = new AccessToken(
$twilioAccountSid,
$twilioApiKey,
$twilioApiSecret,
3600,
$identity
);

// Create Sync grant
$syncGrant = new SyncGrant();

// Create a "grant" which enables a client to use Sync as a given user
$syncGrant->setServiceSid($twilioSyncService);


// Add grant to token
$token->addGrant($syncGrant);

// render token to string
echo $token->toJWT();
30 changes: 30 additions & 0 deletions rest/access-tokens/sync-example/sync-example.5.x.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'twilio-ruby'

# Required for any Twilio Access Token
# To set up environmental variables, see http://twil.io/secure
account_sid = ENV['TWILIO_ACCOUNT_SID']
api_key = ENV['TWILIO_API_KEY']
api_secret = ENV['TWILIO_API_KEY_SECRET']
twilio_sync_service = ENV['TWILIO_SYNC_SERVICE_SID']


# Required for Sync
identity = 'user'

# Create Sync grant for our token
grant = Twilio::JWT::AccessToken::SyncGrant.new

# Create a "grant" which enables a client to use Sync as a given user
grant.service_sid = twilio_sync_service

# Create an Access Token
token = Twilio::JWT::AccessToken.new(
account_sid,
api_key,
api_secret,
[grant],
identity: identity
)

# Generate the token
puts token.to_jwt
27 changes: 27 additions & 0 deletions rest/access-tokens/sync-example/sync-example.6.x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from twilio.jwt.access_token import AccessToken
from twilio.jwt.access_token.grants import SyncGrant

# required for all twilio access tokens
# To set up environmental variables, see http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
api_key = os.environ['TWILIO_API_KEY']
api_secret = os.environ['TWILIO_API_KEY_SECRET']
twilio_sync_service = os.environ[process.env.['TWILIO_SYNC_SERVICE_SID']

# required for Sync grant
identity = 'user'

# Create access token with credentials
token = AccessToken(account_sid, api_key, api_secret, identity=identity)

# Create a Sync grant and add to token
sync_grant = SyncGrant(
# Create a "grant" which enables a client to use Sync as a given user
service_sid=twilio_sync_service
)

token.add_grant(sync_grant)

# Return token info as JSON
print(token.to_jwt())
34 changes: 34 additions & 0 deletions rest/access-tokens/sync-example/sync-example.8.x.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import com.twilio.jwt.accesstoken.AccessToken;
import com.twilio.jwt.accesstoken.SyncGrant;

public class TokenGenerator {

public static void main(String[] args) {
// Get your Account SID from https://twilio.com/console
// To set up environment variables, see http://twil.io/secure
// Required for all types of tokens
String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID");
String twilioApiKey = System.getenv("TWILIO_API_KEY");
String twilioApiSecret = System.getenv("TWILIO_API_SECRET");
String twilioSyncService = System.getenv("TWILIO_SYNC_SERVICE_SID");


// Required for Sync
String identity = "user";

// Create Sync grant
SyncGrant grant = new SyncGrant();

// Create a "grant" which enables a client to use Sync as a given user
grant.setServiceSid(twilioSyncService);

// Create access token
AccessToken token = new AccessToken.Builder(
twilioAccountSid,
twilioApiKey,
twilioApiSecret
).identity(identity).grant(grant).build();

System.out.println(token.toJwt());
}
}