Skip to main content
Android
iOS
Web
Electron

Authenticate REST calls

Before using RESTful API, set up REST authentication. The following REST authentication methods are available:

  • AccessToken2 authentication

    Fill in the Authorization field with agora token= followed by the Signaling SDK AccessToken2 generated from your server.

  • AccessToken authentication

    You need to fill the x-agora-token and x-agora-uid fields in the request header with the following information:

    • The Signaling SDK AccessToken generated from your server
    • The Signaling SDK user ID used to generate the Signaling token

    Implement HTTP basic authentication or token authentication on the server; otherwise, you may encounter the risk of data leakage.

info

Implement authentication on the server to mitigate the risk of data leakage.

Implement token authentication

  1. Generate the token for your app.

  2. Enter the Signaling token and the Signaling user ID into the x-agora-token and x-agora-uid fields of the HTTP request header, respectively.

AccessToken2 authentication sample code

The following sample codes implement AccessToken2 authentication and send a request with the Signaling RESTful API to get Signaling user events.

package mainimport (        "fmt"        "io/ioutil"        "net/http")func main() {        if err := tokenAuthExamle(); err != nil {                panic(err)        }}// Token authentication example in Golang using the Signaling user events RESTful APIfunc tokenAuthExamle() error {        var (                // Signaling Token                tokenValue = "input the token value here"                // App ID                appID     = "input your app ID here"                urlstr    = fmt.Sprintf("https://api.agora.io/dev/v2/project/%s/rtm/vendor/user_events", appID)                authValue = fmt.Sprintf("agora token=%s", tokenValue)        )        // Create request object        req, err := http.NewRequest(http.MethodGet, urlstr, nil)        if err != nil {                return fmt.Errorf("failed to new http request, %w", err)        }        // Set Authorization header        req.Header.Set("Authorization", authValue)        req.Header.Set("Content-Type", "application/json")        // Send request        resp, err := http.DefaultClient.Do(req)        if err != nil {                return fmt.Errorf("failed to send request, %w", err)        }        defer resp.Body.Close()        // Read response body        body, err := ioutil.ReadAll(resp.Body)        if err != nil {                return fmt.Errorf("failed to read response body, %w", err)        }        // Respond status code        if resp.StatusCode/100 != 2 {                return fmt.Errorf("StatusCode(%d) != 2xx, %s", resp.StatusCode, string(body))        }        // Print response body        fmt.Println(string(body))        return nil}

AccessToken authentication sample code

The following sample codes implement AccessToken authentication and send a request with the Signaling RESTful API to get Signaling user events.

import java.io.IOExceptionimport java.net.URIimport java.net.http.HttpClientimport java.net.http.HttpRequestimport java.net.http.HttpResponse// Token authentication example in Kotlin using the Signaling user events RESTful APIfun main() {// Signaling Tokenval token = "Your Signaling token"// User ID used to generate the Signaling tokenval uid = "test_user"    val client = HttpClient.newHttpClient()    // Create request object    val request = HttpRequest.newBuilder()        .uri(URI.create("https://api.agora.io/dev/v2/project/<Your App ID>/rtm/vendor/user_events"))        .GET()        // Add the x-agora-token field to the header        .header("x-agora-token", token)        // Add the x-agora-uid field to the header        .header("x-agora-uid", uid)        .header("Content-Type", "application/json")        .build()    // Send request    val response = client.send(request, HttpResponse.BodyHandlers.ofString())    println(response.body())}
vundefined