Generate tokens on your app server
Updated
Generate whiteboard tokens on your app server by using the official sample code and access keys.
Interactive Whiteboard uses a set of tokens for user authentication: SDK Tokens, Room Tokens, and Task Tokens. Each type of token can be assigned to an admin, writer, or reader role. For details, see Token Overview.
Agora provides an open source netless-token repository on GitHub that includes code samples for generating tokens using JavaScript, TypeScript, Java, Golang, PHP, Ruby, and C#.
This article introduces how to generate tokens from your app server using these code samples and your access keys (the AK and SK).
To enhance security, do not save or send the AK and SK to your app clients. You should save the AK and SK on the app server, and issue tokens from the app server according to the actual needs of your app use-cases.
Prerequisites
Ensure that you have enabled Interactive Whiteboard for your Agora Console project.
In the netless-token-master/Node/JavaScript folder, you can find:
- An
index.jsfile, which contains the source code for generating tokens. - A
README.mdfile, which contains code samples for generating tokens.
Before proceeding, ensure that you have installed the latest version of Node.js LTS.
Generate an SDK Token
Refer to the following steps to generate an SDK Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/Node/JavaScriptfolder and run the following command to install Node.js dependencies:npm install -
Create a file named
sdktoken.jsand copy the following code into it:const { sdkToken, TokenPrefix } = require("./index"); // Generate an SDK Token const netlessSDKToken = sdkToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire { role: 0 // Define the permissions granted by the token. You can set it to 0 (Admin), 1 (Writer), or 2 (Reader) } ); console.log(netlessSDKToken) -
Run the following command, after which you should see a token prefixed with
NETLESSSDK_in the terminal:node sdktoken.js
Generate a Room Token
Refer to the following steps to generate a Room Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/Node/JavaScriptfolder and run the following command to install Node.js dependencies:npm install -
Create a file named
roomtoken.jsand copy the following code into it:const { roomToken, TokenPrefix } = require("./index"); // Generate a Room token const netlessRoomToken = roomToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire { role: 1 // Define the permissions granted by the token. You can set it to 0 (Admin), 1 (Writer), or 2 (Reader) uuid: "Room UUID" // Fill in the Room UUID. You can get it by calling the RESTful API to create a room or get a room list } ); console.log(netlessRoomToken) -
Run the following command, after which you should see a token prefixed with
NETLESSROOM_in the terminal:node roomtoken.js
Generate a Task Token
Refer to the following steps to generate a Task Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/Node/JavaScriptfolder and run the following command to install Node.js dependencies:npm install -
Create a file named
tasktoken.js, and copy the following code into it:const { taskToken, TokenPrefix } = require("./index"); // Generate a Task Token const netlessTaskToken = taskToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire { role: 1 // Define the permissions granted by the token. You can set it to 0 (Admin), 1 (Writer), or 2 (Reader) uuid: "Task UUID" // Fill in the Task UUID. You can get it by calling the RESTful API to start a file-conversion task } ); console.log(netlessTaskToken) -
Run the following command, after which you should see a token prefixed with
NETLESSTASK_in the terminal:node tasktoken.js
In the netless-token-master/Node/TypeScript folder, you can find:
- A
src/index.tsfile, which contains the source code for generating Tokens. - A
README.mdfile, which contains code samples for generating tokens.
Before proceeding, ensure that you have installed the latest version of Node.js LTS.
Generate an SDK Token
Refer to the following steps to generate an SDK Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/Node/TypeScriptfolder and run the following command to install TypeScript:npm install -g typescript -
Create a file named
sdktoken.tsand copy the following code into it:const netlessSDKToken = sdkToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire { role: TokenRole.Admin // Define the permissions granted by the token. You can set it to TokenRole.Admin, TokenRole.Writer, or TokenRole.Reader } ); console.log(netlessSDKToken) -
Run the following command to generate the corresponding
sdktoken.jsfile:tsc sdktoken.ts -
Run the following command, after which you should see a token prefixed with
NETLESSSDK_in the terminal:node sdktoken.js
Generate a Room Token
Refer to the following steps to generate a Room Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/Node/TypeScriptfolder and run the following command to install TypeScript:npm install -g typescript -
Create a file named
roomtoken.tsand copy the following code into it:const netlessRoomToken = roomToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire { role: TokenRole.Admin // Define the permissions granted by the token. You can set it to TokenRole.Admin, TokenRole.Writer, or TokenRole.Reader uuid: "Room UUID" // Fill in the Room UUID. You can get it by calling the RESTful API to create a room or get a room list } ); console.log(netlessRoomToken) -
Run the following command to generate the corresponding
roomtoken.jsfile:tsc roomtoken.ts -
Run the following command, after which you should see a token prefixed with
NETLESSROOM_in the terminal:node roomtoken.js
Generate a Task Token
Refer to the following steps to generate a Task Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/Node/TypeScriptfolder and run the following command to install TypeScript:npm install -g typescript -
Create a file named
tasktoken.tsand copy the following code into it:const netlessTaskToken = taskToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire { role: TokenRole.Writer // Define the permissions granted by the token. You can set it to TokenRole.Admin, TokenRole.Writer, or TokenRole.Reader uuid: "Task UUID" // Fill in the Task UUID. You can get it by calling the RESTful API to start a file conversion task } ); console.log(netlessTaskToken) -
Run the following command to generate the corresponding
tasktoken.jsfile:tsc tasktoken.ts -
Run the following command, after which you should see a token prefixed with
NETLESSTASK_in the terminal:node tasktoken.js
In the netless-token-master/Java folder, you can find:
- A
Token.javafile, which contains the source code for generating tokens. - A
README.mdfile, which contains code samples for generating tokens.
Before proceeding, ensure that you have installed a Java Development Kit.
Generate an SDK Token
Refer to the following steps to generate an SDK Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/Javafolder and add the following code to theToken.javafile:public static void main(String[] args) throws Exception { Map<String, String> map = new HashMap<>(); // Define the permissions granted by the token. You can set it to TokenRole.Admin, TokenRole.Writer, or TokenRole.Reader map.put("role", Token.TokenRole.Admin.getValue()); String sdkToken = Token.sdkToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire map); System.out.println(sdkToken); } -
Go to the directory of the
Token.javafile and run the following command:javac Token.java -
Run the following command, after which you should see a token prefixed with
NETLESSSDK_in the terminal:java Token
Generate a Room Token
Refer to the following steps to generate a Room Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/Javafolder and add the following code to theToken.javafile:public static void main(String[] args) throws Exception { Map<String, String> map = new HashMap<>(); // Define the permissions granted by the token. You can set it to TokenRole.Admin, TokenRole.Writer, or TokenRole.Reader map.put("role", Token.TokenRole.Reader.getValue()); // Fill in the Room UUID. You can get it by calling the RESTful API to create a room or get a room list map.put("uuid", "Your Room UUID"); String roomToken = Token.roomToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire map); System.out.println(roomToken); } -
Go to the directory of the
Token.javafile and run the following command:javac Token.java -
Run the following command, after which you should see a token prefixed with
NETLESSROOM_in the terminal:java Token
Generate a Task Token
Refer to the following steps to generate a Task Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/Javafolder and add the following code to theToken.javafile:public static void main(String[] args) throws Exception { Map<String, String> map = new HashMap<>(); // Define the permissions granted by the token. You can set it to TokenRole.Admin, TokenRole.Writer, or TokenRole.Reader map.put("role", Token.TokenRole.Writer.getValue()); // Fill in the Task UUID. You can get it by calling the RESTful API to start a file-conversion task map.put("uuid", "Your Task UUID"); String taskToken = Token.taskToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire map); System.out.println(taskToken); } -
Go to the directory of the
Token.javafile and run the following command:javac Token.java -
Run the following command, after which you should see a token prefixed with
NETLESSTASK_in the terminal:java Token
In the netless-token-master/golang folder, you can find:
- A
Token.gofile, which contains the source code for generating tokens. - A
README.mdfile, which contains code samples for generating tokens.
Before proceeding, ensure that you have installed the latest version of Golang.
Generate an SDK Token
Refer to the following steps to generate an SDK Token:
-
Download the netless-token repository or clone it to a local directory.
-
Create a file named
sdktoken.goand copy the following code into it:package main import ( "fmt" "../golang" // Replace ../golang with the path to the netless-token folder in your local directory ) func main() { c := token.SDKContent{ // Define the permissions granted by the token. You can set it to token.AdminRole, token.ReaderRole, or token.WriterRole Role: token.AdminRole, } netlessSDKToken := token.SDKToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire &c, ) fmt.Println(netlessSDKToken) } -
Go to the directory of the
sdktoken.gofile and run the following command, after which you should see a token prefixed withNETLESSSDK_in the terminal:go sdktoken.go
Generate a Room Token
Refer to the following steps to generate a Room Token:
-
Download the netless-token repository or clone it to a local directory.
-
Create a file named
roomtoken.goand copy the following code into it:package main import ( "fmt" "../golang" // Replace ../golang with the path to the netless-token folder in your local directory ) func main() { c := token.RoomContent{ // Define the permissions granted by the token. You can set it to token.AdminRole, token.ReaderRole, or token.WriterRole Role: token.AdminRole, // Fill in the Room UUID. You can get it by calling the RESTful API to create a room or get a room list Uuid: "Your Room UUID", } netlessRoomToken := token.RoomToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire &c, ) fmt.Println(netlessRoomToken) } -
Go to the directory of the
roomtoken.gofile and run the following command, after which you should see a token prefixed withNETLESSROOM_in the terminal:go roomtoken.go
Generate a Task Token
Refer to the following steps to generate a Task Token:
-
Download the netless-token repository or clone it to a local directory.
-
Create a file named
tasktoken.goand copy the following code into it:package main import ( "fmt" "../golang" // Replace ../golang with the path to the netless-token folder in your local directory ) func main() { c := token.TaskContent{ // Define the permissions granted by the token. You can set it to token.AdminRole, token.ReaderRole, or token.WriterRole Role: token.WriterRole, // Fill in the Task UUID. You can get it by calling the RESTful API to start a file-conversion task Uuid: "Task UUID", } netlessTaskToken := token.TaskToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire &c, ) fmt.Println(netlessTaskToken) } -
Go to the directory of the
tasktoken.gofile and run the following command, after which you should see a token prefixed withNETLESSTASK_in the terminal:go tasktoken.go
In the netless-token-master/php folder, you can find:
- A
Generate.phpfile, which contains the source code for generating tokens. - A
README.mdfile, which contains code samples for generating tokens.
Before proceeding, ensure that you have installed PHP 7.3 or later.
Generate an SDK Token
Refer to the following steps to generate an SDK Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/phpfolder, create a file namedsdktoken.php, and copy the following code into it:<?php // Import Composer to manage dependencies require __DIR__ . '/vendor/autoload.php'; use Netless\Token\Generate; $netlessToken = new Generate; $sdkToken = $netlessToken->sdkToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire array( "role" => Generate::AdminRole, // Define the permissions granted by the token. You can set it to AdminRole, WriterRole, or ReaderRole ) ); echo $sdkToken; -
Run the following command, after which you should see a token prefixed with
NETLESSSDK_in the terminal:php sdktoken.php
Generate a Room Token
Refer to the following steps to generate a Room Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/phpfolder, create a file namedroomtoken.php, and copy the following code into it:<?php // Import Composer to manage dependencies require __DIR__ . '/vendor/autoload.php'; use Netless\Token\Generate; $netlessToken = new Generate; $roomToken = $netlessToken->roomToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire array( "role" => Generate::ReaderRole, // Define the permissions granted by the token. You can set it to AdminRole, WriterRole, or ReaderRole "uuid" => "Your Room UUID" // Fill in the Room UUID. You can get it by calling the RESTful API to create a room or get a room list ) ); echo $roomToken; -
Run the following command, after which you should see a token prefixed with
NETLESSROOM_in the terminal:php roomtoken.php
Generate a Task Token
Refer to the following steps to generate a Task Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/phpfolder, create a file namedtasktoken.php, and copy the following code into it:<?php // Import Composer to manage dependencies require __DIR__ . '/vendor/autoload.php'; use Netless\Token\Generate; $netlessToken = new Generate; $roomToken = $netlessToken->roomToken( "Your AK", // Fill in the AK you get from Agora Console "Your SK", // Fill in the SK you get from Agora Console 1000 * 60 * 10, // Token validity period in milliseconds. If you set it to 0, the token will never expire array( "role" => Generate::ReaderRole, // Define the permissions granted by the token. You can set it to AdminRole, WriterRole, or ReaderRole "uuid" => "Your Task UUID" // Fill in the Task UUID. You can get it by calling the RESTful API to start a file-conversion task ) ); echo $sdkToken; -
Run the following command, after which you should see a token prefixed with
NETLESSTASK_in the terminal:php tasktoken.php
In the netless-token-master/ruby folder, you can find:
- A
token.rbfile, which contains the source code for generating tokens. - A
README.mdfile, which contains code samples for generating tokens.
Before proceeding, ensure that you have installed Ruby 2.1 or later.
Generate an SDK Token
Refer to the following steps to generate an SDK Token:
-
Download the netless-token repository, or clone it to a local directory.
-
Go to the
netless-token-master/rubyfolder and run the following command to installuuidtools:gem install uuidtools -
In the
rubyfolder, create a file namedsdktoken.rband copy the following code into it:require './lib/token.rb' sdktoken = NetlessToken.sdk_token( "Your AK", # Fill in the AK you get from Agora Console "Your SK", # Fill in the SK you get from Agora Console 1000 * 60 * 10, # Token validity period in milliseconds. If you set it to 0, the token will never expire { :role => NetlessToken::ROLE::ADMIN # Define the permissions granted by the token. You can set it to ADMIN, WRITER, or READER } ) puts sdktoken -
Run the following command, after which you should see a token prefixed with
NETLESSSDK_in the terminal:ruby sdktoken.rb
Generate a Room Token
Refer to the following steps to generate a Room Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/rubyfolder and run the following command to installuuidtools:gem install uuidtools -
In the
rubyfolder, create a file namedroomtoken.rband copy the following code into it:require './lib/token.rb' roomtoken = NetlessToken.room_token( "Your AK", # Fill in the AK you get from Agora Console "Your SK", # Fill in the SK you get from Agora Console 1000 * 60 * 10, # Token validity period in milliseconds. If you set it to 0, the token will never expire { :role => NetlessToken::ROLE::ADMIN # Define the permissions granted by the token. You can set it to ADMIN, WRITER, or READER :uuid => "Your Room UUID" # Fill in the Room UUID. You can get it by calling the RESTful API to create a room or get a room list } ) puts roomtoken -
Run the following command, after which you should see a token prefixed with
NETLESSROOM_in the terminal:ruby roomtoken.rb
Generate a Task Token
Refer to the following steps to generate a Task Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/rubyfolder, and run the following command to installuuidtools:gem install uuidtools -
In the
rubyfolder, create a file namedtasktoken.rband copy the following code into it:require './lib/token.rb' tasktoken = NetlessToken.task_token( "Your AK", # Fill in the AK you get from Agora Console "netless sk", # Fill in the SK you get from Agora Console 1000 * 60 * 10, # Token validity period in milliseconds. If you set it to 0, the token will never expire { :role => NetlessToken::ROLE::ADMIN # Define the permissions granted by the token. You can set it to ADMIN, WRITER, or READER :uuid => "Your Room UUID" # Fill in the Task UUID. You can get it by calling the RESTful API to start a file-conversion task } ) puts tasktoken -
Run the following command, after which you should see a token prefixed with
NETLESSTASK_in the terminal:ruby tasktoken.rb
In the netless-token-master/csharp folder, you can find:
- A
Token.csfile, which contains the source code for generating tokens. - A
README.mdfile, which contains code samples for generating tokens.
Before proceeding, ensure that you have installed the latest version of Visual Studio.
Generate an SDK Token
Refer to the following steps to generate an SDK Token:
-
Download the netless-token repository, or clone it to a local directory.
-
Go to the
netless-token-master/csharpfolder and open thecsharp.slnfile in Visual Studio. -
Fill in your AK, SK, token validity period, and token role in the
Program.csfile.using System; using Netless; class Program { static void Main(string[] args) { string token = NetlessToken.SdkToken( // Fill in the AK you get from Agora Console "ak", // Fill in the SK you get from Agora Console "sk", // Set the Token validity period. If you set it to 0, the token will never expire 1000 * 60 * 10, // Define the permissions granted by the token. You can set it to TokenRole.Admin, TokenRole.Writer, or TokenRole.Reader new SdkContent(TokenRole.Admin)); Console.WriteLine(token); } } -
Run the project in Visual Studio. You should see a token prefixed with
NETLESSSDK_in the terminal.
Generate a Room Token
Refer to the following steps to generate a Room Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/csharpfolder and open thecsharp.slnfile in Visual Studio. -
Delete the code in the
Program.csfile and copy the following sample code into it:using System; using Netless; class Program { static void Main(string[] args) { string token = NetlessToken.RoomToken( // Fill in the AK you get from Agora Console "ak", // Fill in the SK you get from Agora Console "sk", // Set the Token validity period. If you set it to 0, the token will never expire 1000 * 60 * 10, // Define the permissions granted by the token. You can set it to TokenRole.Admin, TokenRole.Writer, or TokenRole.Reader // Fill in the Room UUID. You can get it by calling the RESTful API to create a room or get a room list new RoomContent(TokenRole.Admin, "房间的 UUID") ); Console.WriteLine(token); } } -
Run the project in Visual Studio. You should see a token prefixed with
NETLESSROOM_in the terminal.
Generate a Task Token
Refer to the following steps to generate a Task Token:
-
Download the netless-token repository or clone it to a local directory.
-
Go to the
netless-token-master/csharpfolder and open thecsharp.slnfile in Visual Studio. -
Delete the code in the
Program.csfile and copy the following sample code into it:using System; using Netless; class Program { static void Main(string[] args) { string token = NetlessToken.RoomToken( // Fill in the AK you get from Agora Console "ak", // Fill in the SK you get from Agora Console "sk", // Set the Token validity period. If you set it to 0, the token will never expire 1000 * 60 * 10, // Define the permissions granted by the token. You can set it to TokenRole.Admin, TokenRole.Writer, or TokenRole.Reader // Fill in the Task UUID. You can get it by calling the RESTful API to start a file-conversion task new TaskContent(TokenRole.Admin, "Your Task UUID") ); Console.WriteLine(token); } } -
Run the project in Visual Studio. You should see a token prefixed with
NETLESSTASK_in the terminal.
