メニュー

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

ユーザーアイデンティティとアクセストークン

概要

An Access Token controls Participant identity and Room permissions in your Programmable Video application. Read below to learn more.

About Access Tokens

Access Tokens are short-lived credentials that are signed with a Twilio API Key Secret and contain grants which govern the actions the client holding the token is permitted to perform. All Twilio Access Tokens must include the following information:

  • A Twilio Account SID, which is the public identifier of the Twilio account associated with the Access Token.
  • An API Key SID, which is the public identifier of the key used to sign the token.
  • An Identity grant, which sets the Twilio user identifier for the client holding the token.
  • The API Key Secret associated with the API Key SID is used to sign the Access Token and verify that it is associated with your Twilio account.

Programmable Video Access Tokens also include the following information:

  • A mandatory Video grant, which indicates the holder of the Access Token can access Programmable Video services.
  • Optionally, a Room grant (contained within the Video grant) for a specific Room name or SID, which indicates the holder of the Access Token may only connect to the indicated Room.

Access Tokens are based on the JSON Web Token standard (RFC 7519).

Limit Room Access

The Room grant allows you to scope a Participant's access to a single Room. When a Participant connects with a token that contains a Room grant, the value is compared against:

  1. The Room's UniqueName.
  2. The Room's Sid.

For example, if the Access Token contains a Room grant for DailyStandup, the client holding this Access Token will only be allowed to connect to the Room with the UniqueName property DailyStandup.

See below for working examples.

Note: If the Room Grant is scoped to a Room that has been created using the REST API with the EnableTurn property set to true, then media may be relayed over TURN servers, if required.

Time-To-Live (ttl)

Access Tokens must be valid while joining a Room and when reconnecting to a Room due to network disruption or handoff. Therefore, we recommend that you set the ttl to the maximum allowed session length, which is currently 14,400 seconds (4 hours).

Generating Access Tokens

You can generate an Access Token in any of the following ways.

Use the Testing Tools page in the Twilio Console

Use the Testing Tools page in the Twilio Console to generate an Access Token. This is useful when prototyping a new application or learning how Access Tokens work.

  1. Visit the Testing Tools page in the Twilio Console.
  2. Enter a value for the token’s Identity field.
  3. Optionally enter a Room name, which will limit the holder of this token’s access to the specified Room.
  4. Click Generate to create the Access Token
  5. Copy and paste the generated token into your application.

Use a Twilio helper library

Use a Twilio helper library to generate an Access Token in your back-end server. The examples below generate an Access Token for the user alice that grants access to the Room DailyStandup:

サンプル

Java
import com.twilio.jwt.accesstoken.AccessToken;
import com.twilio.jwt.accesstoken.VideoGrant;

public class TokenGenerator {

  public static void main(String[] args) {
    // Required for all types of tokens
    String twilioAccountSid = "ACxxxxxxxxxxxx";
    String twilioApiKey = "SKxxxxxxxxxxxx";
    String twilioApiSecret = "xxxxxxxxxxxxxx";

    String identity = "alice";

    // Create Video grant
    VideoGrant grant = new VideoGrant();
    grant.setRoom("DailyStandup");

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

    System.out.println(token.toJwt());
  }
}
JavaScript
const AccessToken = require('twilio').jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;

// Used when generating any kind of Access Token
const twilioAccountSid = 'ACxxxxxxxxxx';
const twilioApiKey = 'SKxxxxxxxxxx';
const twilioApiSecret = 'xxxxxxxxxxxx';

// 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);
token.identity = 'alice';

// Create a Video grant which enables a client to use Video 
// and limits access to the specified Room (DailyStandup)
const videoGrant = new VideoGrant({
    room: 'DailyStandup'
});

// Add the grant to the token
token.addGrant(videoGrant);

// Serialize the token to a JWT string
console.log(token.toJwt());
PHP
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
// Required for all Twilio access tokens
$twilioAccountSid = 'ACxxxxxxxxxxxx';
$twilioApiKey = 'SKxxxxxxxxxxxx';
$twilioApiSecret = 'xxxxxxxxxxxxxx';

// A unique identifier for this user
$identity = "alice";
// The specific Room we'll allow the user to access
$roomName = 'DailyStandup';

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

// Create Video grant
$videoGrant = new VideoGrant();
$videoGrant->setRoom($roomName);

// Add grant to token
$token->addGrant($videoGrant);
// render token to string
echo $token->toJWT();
Python
from twilio.jwt.access_token import AccessToken
from twilio.jwt.access_token.grants import VideoGrant

# required for all twilio access tokens
account_sid = 'ACxxxxxxxxxxxx'
api_key = 'SKxxxxxxxxxxxx'
api_secret = 'xxxxxxxxxxxxxx'

identity = 'alice'

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

# Create a Video grant and add to token
video_grant = VideoGrant(room='DailyStandup')
token.add_grant(video_grant)

# Return token info as JSON
print(token.to_jwt())
Ruby
require 'twilio-ruby'

# Required for any Twilio Access Token
account_sid = 'ACxxxxxxxxxxxx'
api_key = 'SKxxxxxxxxxxxx'
api_secret = 'xxxxxxxxxxxxxx'

identity = 'alice'

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

# Create Video grant for our token
grant = Twilio::JWT::AccessToken::VideoGrant.new
grant.room = 'DailyStandup'
token.add_grant(grant)

# Generate the token
puts token.to_jwt
C#
using System;
using System.Collections.Generic;
using Twilio.Jwt.AccessToken;

class Example
{
  static void Main(string[] args)
  {
    // Substitute your Twilio AccountSid and ApiKey details
    var accountSid = "accountSid";
    var apiKeySid = "apiKeySid";
    var apiKeySecret = "apiKeySecret";

    var identity = "example-user";

    // Create a video grant for the token
    var grant = new VideoGrant();
    grant.Room = "cool room";
    var grants = new HashSet<IGrant> { grant };

    // Create an Access Token generator
    var token = new Token(accountSid, apiKeySid, apiKeySecret, identity: identity, grants: grants);

    // Serialize the token as a JWT
    Console.WriteLine(token.ToJwt());
  }
}

アクセストークンサーバーサンプルアプリケーション

サンプルアプリケーションは様々なプログラミング言語によるアクセストークンの生成について実演しています。

Kedar Toraskar Aymen Naim Luis Lopez Chris Eagleston Manjesh Malavalli
Rate this page:

ヘルプが必要ですか?

誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。