|
| 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 | +} |
0 commit comments