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

Commit 616f865

Browse files
Add examples for adding a PlaybackGrant to an Access Token. (#944)
1 parent 59fd573 commit 616f865

File tree

7 files changed

+230
-0
lines changed

7 files changed

+230
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const AccessToken = require('twilio').jwt.AccessToken;
2+
const PlaybackGrant = AccessToken.PlaybackGrant;
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+
// Create an access token which you will sign and return to the client,
11+
// containing the grant you will create in the next steps
12+
const token = new AccessToken(
13+
twilioAccountSid,
14+
twilioApiKey,
15+
twilioApiSecret
16+
);
17+
18+
// Create a PlaybackGrant resource for a specific PlayerStreamer
19+
// via the REST API
20+
const client = require('twilio')(twilioApiKey, twilioApiSecret, {
21+
accountSid: twilioAccountSid });
22+
client.media.playerStreamer('VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
23+
.playbackGrant()
24+
.create({ttl: 60})
25+
.then(playbackGrant => {
26+
// Wrap the PlaybackGrant that you received from the REST API
27+
// in a PlaybackGrant object and then attach that wrapped
28+
// grant to your Access Token
29+
const wrappedPlaybackGrant = new PlaybackGrant({
30+
grant: playbackGrant.grant
31+
});
32+
token.addGrant(wrappedPlaybackGrant);
33+
// Serialize the token to a JWT string
34+
console.log(token.toJwt());
35+
}
36+
);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Twilio;
4+
using Twilio.Rest.Api.V2010;
5+
using Twilio.Rest.Media.V1.PlayerStreamer;
6+
using Twilio.Jwt.AccessToken;
7+
using Newtonsoft.Json;
8+
9+
10+
class Example
11+
{
12+
static void Main(string[] args)
13+
{
14+
// These values are necessary for any access token
15+
// To set up environmental variables, see http://twil.io/secure
16+
string twilioAccountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
17+
string twilioApiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");
18+
string twilioApiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");
19+
20+
// Create a PlaybackGrant resource for a specific PlayerStreamer
21+
// via the REST API. The pathSid value should be the PlayerStreamer SID.
22+
TwilioClient.Init(twilioApiKey, twilioApiSecret, twilioAccountSid);
23+
var playbackGrant = PlaybackGrantResource.Create(
24+
ttl: 60,
25+
pathSid: "VJ3f3dc19da15af020bb395f0487f5221d"
26+
);
27+
28+
// Serialize the returned grant into <Dictionary<string, object>>
29+
var json = JsonConvert.SerializeObject(playbackGrant.Grant);
30+
var grantDictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
31+
32+
// Wrap the grant you received from the API
33+
// in a PlaybackGrant object and then attach that wrapped
34+
// grant to your Access Token
35+
var wrappedPlaybackGrant = new PlaybackGrant();
36+
Console.WriteLine(playbackGrant);
37+
wrappedPlaybackGrant.Grant = grantDictionary;
38+
39+
var grants = new HashSet<IGrant> { wrappedPlaybackGrant };
40+
41+
// Create an Access Token generator and attach the grants
42+
var token = new Token(
43+
twilioAccountSid,
44+
twilioApiKey,
45+
twilioApiSecret,
46+
grants: grants);
47+
48+
Console.WriteLine(token.ToJwt());
49+
}
50+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\PlaybackGrant;
6+
use Twilio\Rest\Client;
7+
8+
// Required for all Twilio access tokens
9+
// To set up environmental variables, see http://twil.io/secure
10+
$twilioAccountSid = getenv('TWILIO_ACCOUNT_SID');
11+
$twilioApiKey = getenv('TWILIO_API_KEY');
12+
$twilioApiSecret = getenv('TWILIO_API_KEY_SECRET');
13+
14+
$twilio = new Client($twilioApiKey, $twilioApiSecret, $twilioAccountSid);
15+
16+
// Create access token, which we will serialize and send to the client
17+
$token = new AccessToken(
18+
$twilioAccountSid,
19+
$twilioApiKey,
20+
$twilioApiSecret,
21+
3600
22+
);
23+
24+
//Create a PlaybackGrant resource for a specific PlayerStreamer
25+
// via the REST API
26+
$playbackGrant = $twilio->media->v1->playerStreamer("VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
27+
->playbackGrant()
28+
->create(["ttl" => 60]);
29+
30+
// Wrap the grant you received from the API
31+
// in a PlaybackGrant object and then attach that wrapped
32+
// grant to your Access Token
33+
$wrappedPlaybackGrant = new PlaybackGrant();
34+
$wrappedPlaybackGrant->setGrant($playbackGrant->grant);
35+
36+
// Add grant to token
37+
$token->addGrant($wrappedPlaybackGrant);
38+
39+
// render token to string
40+
echo $token->toJWT();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
# Create a PlaybackGrant resource for a specific PlayerStreamer
10+
# via the REST API
11+
@client = Twilio::REST::Client.new(api_key, api_secret, account_sid)
12+
playback_grant = @client.media
13+
.player_streamer('VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
14+
.playback_grant
15+
.create(ttl: 60)
16+
17+
# Wrap the grant you received from the API
18+
# in a PlaybackGrant object and then attach that wrapped
19+
# grant to your Access Token
20+
wrapped_playback_grant = Twilio::JWT::AccessToken::PlaybackGrant.new
21+
wrapped_playback_grant.grant = playback_grant.grant
22+
23+
# Create an Access Token and attach the wrapped PlaybackGrant
24+
token = Twilio::JWT::AccessToken.new(
25+
account_sid,
26+
api_key,
27+
api_secret,
28+
[wrapped_playback_grant]
29+
)
30+
31+
# Generate the token
32+
puts token.to_jwt
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
from twilio.jwt.access_token import AccessToken
3+
from twilio.jwt.access_token.grants import PlaybackGrant
4+
from twilio.rest import Client
5+
6+
# Required for all Twilio Access Tokens
7+
# To set up environmental variables, see http://twil.io/secure
8+
account_sid = os.environ['TWILIO_ACCOUNT_SID']
9+
api_key = os.environ['TWILIO_API_KEY']
10+
api_secret = os.environ['TWILIO_API_KEY_SECRET']
11+
12+
# Create Access Token with credentials
13+
token = AccessToken(account_sid, api_key, api_secret)
14+
15+
# Create a PlaybackGrant resource for a specific PlayerStreamer
16+
# via the REST API
17+
client = Client(api_key, api_secret, account_sid)
18+
playback_grant = client.media \
19+
.player_streamer('VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
20+
.playback_grant() \
21+
.create(ttl=60)
22+
23+
24+
# Wrap the grant you received from the API
25+
# in a PlaybackGrant object and then attach that wrapped
26+
# grant to your Access Token
27+
wrapped_grant = PlaybackGrant(grant=playback_grant.grant)
28+
token.add_grant(PlaybackGrant(grant=wrapped_grant))
29+
30+
# Return token info as JSON
31+
print(token.to_jwt())
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import com.twilio.Twilio;
2+
import com.twilio.jwt.accesstoken.AccessToken;
3+
import com.twilio.jwt.accesstoken.PlaybackGrant;
4+
5+
public class TokenGenerator {
6+
7+
public static void main(String[] args) {
8+
// Get your Account SID from https://twilio.com/console
9+
// To set up environment variables, see http://twil.io/secure
10+
// Required for all types of tokens
11+
String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID");
12+
String twilioApiKey = System.getenv("TWILIO_API_KEY");
13+
String twilioApiSecret = System.getenv("TWILIO_API_SECRET");
14+
15+
// Create a PlaybackGrant resource for a specific PlayerStreamer
16+
// via the REST API
17+
Twilio.init(twilioApiKey, twilioApiSecret, twilioAccountSid);
18+
com.twilio.rest.media.v1.playerstreamer.PlaybackGrant playbackGrant =
19+
com.twilio.rest.media.v1.playerstreamer.PlaybackGrant.creator("VJ3f3dc19da15af020bb395f0487f5221d")
20+
.setTtl(60)
21+
.create();
22+
23+
// Wrap the grant you received from the API
24+
// in a PlaybackGrant object and then attach that wrapped
25+
// grant to your Access Token
26+
PlaybackGrant wrappedPlaybackGrant = new PlaybackGrant().setGrant(playbackGrant.getGrant());
27+
28+
// Create access token
29+
AccessToken token = new AccessToken.Builder(
30+
twilioAccountSid,
31+
twilioApiKey,
32+
twilioApiSecret
33+
).grant(wrappedPlaybackGrant).build();
34+
35+
System.out.println(token.toJwt());
36+
}
37+
}
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 (Twilio Live PlaybackGrant)",
3+
"type": "server"
4+
}

0 commit comments

Comments
 (0)