Generate an app token
Authentication is the process of validating identities. Agora uses digital tokens to authenticate users and their privileges before they access an Agora service, such as joining an Agora call, or logging in to Chat.
To securely connect to Chat, you use the following token types:
- User token: User level access to Chat from your app using Chat SDK. User tokens are used to authenticate users when they log in to your Chat application. For details, see Implement an Agora user token server for Chat.
- App token: Admin level access to Chat using the REST API. App tokens grant admin privileges for the app developer to manage the Chat applications, for example, creating and deleting users.
You use Chat app tokens to make authenticated REST calls to Chat. To obtain a Chat app token, you retrieve an Agora app token from your app token server and convert it using the Agora app token obtained to a Chat app token.
Understand the tech
The following figure shows the authentication workflow for Chat REST API:
a Chat app token is valid for a maximum of 24 hours. When you call Chat REST APIs, the Chat server reads the information stored in the token and uses it for authentication. a Chat app token contains the following information:
- The App ID of your Agora project
- The App Certificate of your Agora project
- The valid duration of the token
Preprequisites
-
A valid Agora account.
-
An Agora project with the App Certificate and Chat enabled. See Enable and Configure Chat Service for details.
-
The App ID ,
org_name
andapp_name
of your Agora project, see Enable and Configure Chat.
If your network environment has a firewall, Agora provides firewall whitelists so that you can use Chat with restricted network access. You can contact support@agora.io for more information about the firewall whitelists.
Implement the authentication flow
This section shows how to implement authentication with a Chat app token step by step.
Create an app token server
Token generators create the tokens requested by your app client to enable secure access to Chat. To serve these tokens you create an app token server.
The following example shows how to build and run an app token server written in Java on your local machine. This sample server is for demonstration purposes only. Do not use it in a production environment.
-
Create a Maven project in IntelliJ, set the name of your project, choose the location to save your project, then click Finish.
-
In
<Project name>/pom.xml
, add the following dependencies and then reload the Maven project: -
Import the token builders provided by Agora to this project.
- Download the chat and media packages.
- In the token server project, create a
com.agora.chat.token.io.agora
package under<Project name>/src/main/java
. - Copy the chat and media packages and paste under
com.agora.chat.token.io.agora
. The following figure shows the project structure:
-
Fix the import errors in
chat/ChatTokenBuilder2
andmedia/AccessToken
.-
In
ChatTokenBuilder2
, the import should beimport com.agora.chat.token.io.agora.media.AccessToken2
. -
In
AccessToken
, the import should be as follows:
-
-
In
<Project name>/src/main/resource
, create aapplication.properties
file. You need to update the properties with the value for your Chat project and set the validity period of the Agora app token you request, for example,expire.second=6000
.Note: The validity period of a Chat app token is subject to that of the Agora app token generated by your token server.
For example, if you set the validity period of the Agora app token as 10,000 seconds, and you use this token to convert and obtain a Chat app token after 100 seconds, the Chat app token is to be expired after 9,900 seconds.
For details on how to get the REST API domain, see Enable and Configure Chat.
-
In
com.agora.chat.token
, create a file namedAgoraChatTokenController.java
and copy the following codes into the file: -
In
com.agora.chat.token
, create a Java class namedAgoraChatTokenStarter
and copy the following codes into the file: -
To start the server, click the green triangle button, and select Debug "AgoraChatTokenStarter...".
-
Now you have created an Agora app token server on your local machine. The server returns an Agora app token on GET requests. To test this, paste http://localhost:8090/chat/app/token in your broswer.
You see an Agora app token that resembles:
Call the Chat REST APIs with a Chat app token
This section shows how to create a new user in your Chat using an authenticated call to Chat REST API. To do this you:
-
Retrieve an Agora app token and convert it to a Chat app token. For example:
i. In the terminal, use curl to make a GET request to your Agora app token server:
The return parameters contain the Agora app token. For example:
-
Add the Agora app token returned previously as the
Authorization: Bear
parameter and retrieve a Chat app token:The return parameters contain the Chat app token and the timestamp (ms) when the token expires in a JSON object. For example:
-
Use the Chat app token returned to call the Chat REST API to create a new user:
i. In terminal, use curl to make a POST request to the Chat server. For example:
The return parameters contain the information about the user you just created. For example:
Reference
This section provides additional information about Chat app tokens for your reference.
Request path
The following parameters are required when you send a request for a Chat app token:
Parameter | Data type | Required/Optional | Description |
---|---|---|---|
org_name | String | Required | The unique identification that the Chat assigns to each customer developing with Chat. |
app_name | String | Required | The name that the Chat assigns to each app. |
token | String | Required | The Agora app token. |
Request headers
To convert an Agora app token to a Chat app token, you need to pass in the following parameters in the request header:
Parameter | Data type | Required/Optional | Description |
---|---|---|---|
Content-Type | String | Required | application/json |
Authorization | String | Required | Bearer AgoraappToken |
Request body
Parameter | Data type | Required/Optional | Value |
---|---|---|---|
grant_type | String | Required | agora |
Response body
Parameter | Data type | Value |
---|---|---|
access_token | String | The Chat app token. |
expires_in | Number | The valid duration (in seconds) of the token. |
Example
Request Example
Response Example
Status codes
The possible response status codes are listed below:
Response status code | Description |
---|---|
200 | The request is successful, and the server returns the requested Chat app token and the timestamp when the token expires. |
5xx | The request fails probably because the API to convert an Agora app token to a Chat app token might be rate-limited, or an error occur during the process. |
Other Considerations
If you use Chat together with the Agora RTC SDK, Agora recommends you upgrade to AccessToken2.