RoomKit
  • iOS : Objective-C
  • Android
  • Web
  • Flutter
  • Introduction
  • Demo app
  • Sample codes
  • Client SDKs
    • SDK downloads
    • Release notes
  • Getting started
    • Integration
    • Start a room
  • Guides
    • Customize room parameters
    • Customize the room UI
    • Listen for room callbacks
    • Screen rotation support
    • Screen sharing
    • Manage rooms with Education Cloud Service
    • Use server event callbacks
  • Server APIs
  • Error codes
  • Documentation
  • RoomKit
  • Server APIs
  • RoomKit Server APIs
  • Authentication
  • Get SDK access token

Get SDK access token

Last updated:2022-03-22 13:06

1 Description

Gets the Roomkit access token for authenticating client-side user login.

2 Request method and endpoint

  • Request method: POST

  • Request endpoint:

    Service environment Rate limit Endpoint
    Production 10 requests/second https://roomkit-api.zego.im/auth/get_sdk_token
  • Transmission protocol: HTTPS

3 Request parameters

Parameter Type Required Example Description
sign String Yes "CIE7hNPK6Y55DeUJBQWERTY" Generated using the function md5("%s%s%d%d%d"%(secret_sign[:32].lower(), "device_id", 3, 1, timestamp)))
secret_id Int Yes 12580 The Secret ID assigned to your project by ZEGO.
device_id String Yes "38-F9-D3-87-C8-15" Device ID. Your app client needs to call the deviceID method of ZegoRoomKit SDK to obtain the device ID and pass it to your app server.
timestamp Int yes 1615541262 Expiration time of the signature (the sign parameter), a Unix timestamp in seconds. (e.g., time.now().unix() + 3600, where 3600 is the number of seconds the token remains valid)
common_data.platform Int Yes 0 End-user device platform.
  • 0: None
  • 1: Windows
  • 2: Mac
  • 4: iOS
  • 8: Android
  • 16: MiniProgram
  • 32: Web
  • 64: SDK Server

Parameters used to generate the value of sign:

Parameter Type Required Example Description
secret_sign[:32].lower() String Yes "qwertyuiqwertyuiqwertyuiqwertyui" The first 32 characters of secret_sign.
device_id String Yes "38-F9-D3-87-C8-15" Device ID. Your app client needs to call the deviceID method of ZegoRoomKit SDK to obtain the device ID and pass it to your app server.
verify_type Int Yes 3 The verification method. Defaults to 3, which means the SDK initiates the verification.
version Int Yes 1 Version number. Set it to 1 by default.
timestamp Int Yes 1615541262 A timestamp, in seconds. Just keep it the same as the request parameter timestamp.

4 Sample request

{
    "common_data": {
        "platform": 8
    },
    "sign": "CIE7hNPK6Y55DeUJBQWERTY",
    "secret_id": 12580,
    "device_id": "38-F9-D3-87-C8-15",
    "timestamp": 1615541262
}

5 Response parameters

Parameter Example Type Description
ret - Object The returned status of the request.
ret.code 0 Int The returned code.
ret.msg "succeed" String The returned message.
ret.version "1.0.0" String Version number.
data - Object The returned data of the request.
data.sdk_token "qwertyuiqwertyuiqwe" String The SDK access token returned by the RoomKit server.

6 Sample response

{
    "ret": {
        "code": 0,
        "msg": "succeed",
        "version": "1.0.0"
    },
    "data": {
        "sdk_token": "qwertyuiqwertyuiqwe",
    }
}

7 Sample code

7.1 Sample code in Python

import time
import hashlib
import json
import requests

def get_SDK_token(secret_id, secret_sign, deviceid, platform):
    timestamp = int(time.mktime(time.localtime(time.time()))) + 3600
    signSrc = ("%s%s%d%d%d"% (secret_sign[:32], deviceid, 3, 1, timestamp))
    sign = hashlib.md5(signSrc.encode()).hexdigest()
    url ='https://roomkit-api.zego.im/auth/get_sdk_token'
    data = {
        "common_data": {
            "platform": platform,
        },
        "sign": sign,
        "secret_id": secret_id,
        "device_id": deviceid,
        "timestamp": timestamp
    }
    r = requests.post(url=url, json=data)
    respondStr = r.text.encode('utf-8').decode('utf-8')
    sdk_token = json.loads(respondStr)["data"]["sdk_token"]
    return sdk_token

7.2 Sample code in Golang

import (
    "bytes"
    "crypto/md5"
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "net/http"
    "testing"
    "time"
)

func GetSDKToken(secretId int, secretSign, deviceId string, platform int) (sdkToken string, err error) {
    // Token expiration time
    expired := time.Now().Unix() + 3600

    // Calculate hash value
    m := md5.New()
    m.Write([]byte(fmt.Sprintf("%s%s%d%d%d", secretSign[:32], deviceId, 3, 1, expired)))
    hash := fmt.Sprintf("%x", m.Sum(nil))

    var reqStruct struct{
        CommonData  struct{
            Platform int     `json:"platform"`
        }
        SecretId     int     `json:"secret_id"`
        Sign         string    `json:"sign"`
        DeviceId     string    `json:"device_id"`
        Timestamp    int64   `json:"timestamp"`
    }
    reqStruct.CommonData.Platform = platform
    reqStruct.SecretId = secretId
    reqStruct.Sign = hash
    reqStruct.DeviceId = deviceId
    reqStruct.Timestamp = expired
    reqBody, _ := json.Marshal(reqStruct)

    // Set the POST request to get the SDK access token.
    req, err := http.NewRequest(http.MethodPost, "https://roomkit-api.zego.im/auth/get_sdk_token", bytes.NewBuffer(reqBody))
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    var resp *http.Response
    resp, err = client.Do(req)
    if err != nil {
        return
    }
    defer resp.Body.Close()

    var respBody []byte
    respBody, err = ioutil.ReadAll(resp.Body)
    if err != nil {
        return
    }

    var ret struct{
        Ret struct{
            Code     int     `json:"code"`
            Msg     string    `json:"msg"`
            Version string    `json:"version"`
        }                    `json:"ret"`
        Data struct{
            SDKToken     string    `json:"sdk_token"`
        }
    }

    err = json.Unmarshal(respBody, &ret)
    if err != nil {
        return
    }
    if ret.Ret.Code != 0 {
        return "", errors.New(ret.Ret.Msg)
    }

    return ret.Data.SDKToken, nil
}
Page Directory