AICallKit SDK がコールを開始するために必要な ARTC 認証トークンを生成します。
仕組み
AICallKit SDK は、クライアントから AI エージェントコールを開始します。アプリサーバーで認証トークンを生成し、各コールの前にクライアントがトークンを取得するためのエンドポイントを公開する必要があります。
操作手順
ARTC 認証トークンを生成するには、ARTC アプリケーションの App ID と AppKey が必要です。
-
IMS コンソールにアクセスし、AI エージェントをクリックして詳細ページを開きます。
[基本情報] タブで、[ワークフロー設定] セクションを見つけ、[ARTC アプリケーション] の下に表示されている [RTC App ID] をメモします。
-
RTC App ID をクリックして ApsaraVideo Live コンソールを開き、App ID と AppKey を取得します。
[アプリケーション管理] ページで、アプリケーション名をクリックし、[基本情報] タブに移動して、[App ID] と [AppKey] をコピーします。
-
App ID と AppKey を使用して ARTC 認証トークンを生成します。トークンは次のように計算されます。
// 1. 次のフィールドを連結します: AppID+AppKey+ChannelID+UserID+Nonce+Timestamp // 2. 連結した文字列に sha256 関数を適用してトークンを生成します。 token = sha256(AppID+AppKey+ChannelID+UserID+Nonce+Timestamp) // 例: AppID = "abc",AppKey="abckey",ChannelID="abcChannel",UserID="abcUser",Nonce="",Timestamp=1699423634 token = sha256("abcabckeyabcChannelabcUser1699423634") = "3c9ee8d9f8734f0b7560ed8022a0590659113955819724fc9345ab8eedf84f31"フィールド
説明
AppID
コンソールでアプリケーションを作成した後に自動的に生成される ARTC のアプリケーション ID とキーです。詳細については、「開発パラメーターの取得」をご参照ください。
AppKey
ChannelID
カスタムチャンネル ID です。文字列である必要があり、数字、文字、ハイフン (-)、アンダースコア (_) を含めることができます。最大長は 64 文字です。
同じセッションのすべての参加者 (ホストと共同ホスト) は、同じ
ChannelIDを使用する必要があります。UserId
カスタムユーザー ID です。文字列である必要があり、数字、文字、ハイフン (-)、アンダースコア (_) を含めることができます。最大長は 64 文字です。
Nonce
Nonce 文字列です。このフィールドは空のままにすることを推奨します。
Timestamp
トークンの有効期限のタイムスタンプ (秒単位) です。24 時間に設定することを推奨します。そのためには、現在の UNIX タイムスタンプに 86400 (24 × 60 × 60) を加算します。
-
サーバーサイドのトークン生成: 次のサンプルのいずれかを使用して、アプリサーバーでトークンを生成します。
Java
package com.example; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; import java.util.Calendar; import org.json.JSONObject; public class App { public static String createBase64Token(String appid, String appkey, String channelid, String userid) { // 有効期限タイムスタンプを計算します (現在から 24 時間後) Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.HOUR_OF_DAY, 24); long timestamp = calendar.getTimeInMillis() / 1000; // 文字列を連結します String stringBuilder = appid + appkey + channelid + userid + timestamp; // SHA-256 ハッシュを計算します String token = sha256(stringBuilder); // JSON オブジェクトを作成します JSONObject base64tokenJson = new JSONObject(); base64tokenJson.put("appid", appid); base64tokenJson.put("channelid", channelid); base64tokenJson.put("userid", userid); base64tokenJson.put("nonce", ""); base64tokenJson.put("timestamp", timestamp); base64tokenJson.put("token", token); // JSON オブジェクトを文字列に変換し、Base64 でエンコードします String jsonStr = base64tokenJson.toString(); String base64token = Base64.getEncoder().encodeToString(jsonStr.getBytes(StandardCharsets.UTF_8)); return base64token; } private static String sha256(String input) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8)); StringBuilder hexString = new StringBuilder(); for (byte b : hash) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } public static void main(String[] args) { String appid = "your_appid"; String appkey = "your_appkey"; String channel_id = "your_channel_id"; String user_id = "your_user_id"; String base64token = createBase64Token(appid, appkey, channel_id, user_id); System.out.println("Base64 Token: " + base64token); } }Python
#!/usr/bin/env python # -*- coding: UTF-8 -*- import hashlib import datetime import time import base64 import json def create_base64_token(app_id, app_key, channel_id, user_id): expire = datetime.datetime.now() + datetime.timedelta(days=1) timestamp = int(time.mktime(expire.timetuple())) h = hashlib.sha256() h.update(str(app_id).encode('utf-8')) h.update(str(app_key).encode('utf-8')) h.update(str(channel_id).encode('utf-8')) h.update(str(user_id).encode('utf-8')) h.update(str(timestamp).encode('utf-8')) token = h.hexdigest() jsonToken = {'appid': str(app_id), 'channelid': str(channel_id), 'userid':str(user_id), 'nonce':'', 'timestamp':timestamp, 'token':str(token) } base64Token = base64.b64encode(json.dumps(jsonToken).encode()) return base64Token def main(): app_id = 'your_appid' app_key = 'your_appkey' channel_id = 'your_channel_id' user_id = 'your_user_id' base64Token = create_base64_token(app_id, app_key, channel_id, user_id) print(base64Token) if __name__ == '__main__': main()Go
package main import ( "crypto/sha256" "encoding/base64" "encoding/hex" "encoding/json" "fmt" "time" ) func createBase64Token(appid, appkey, channelID, userID string) (string, error) { // 有効期限タイムスタンプを計算します (現在から 24 時間後) timestamp := time.Now().Add(24 * time.Hour).Unix() // 文字列を連結します stringBuilder := appid + appkey + channelID + userID + fmt.Sprintf("%d", timestamp) // SHA-256 ハッシュを計算します hasher := sha256.New() hasher.Write([]byte(stringBuilder)) token := hasher.Sum(nil) // encoding/hex を使用してハッシュを 16 進数文字列に変換します tokenHex := hex.EncodeToString(token) // JSON オブジェクトを作成します tokenJSON := map[string]interface{}{ "appid": appid, "channelid": channelID, "userid": userID, "nonce": "", "timestamp": timestamp, "token": tokenHex, } // JSON オブジェクトを文字列に変換し、Base64 でエンコードします jsonBytes, err := json.Marshal(tokenJSON) if err != nil { return "", err } base64Token := base64.StdEncoding.EncodeToString(jsonBytes) return base64Token, nil } func main() { appid := "your_appid" appkey := "your_appkey" channelID := "your_channel_id" userID := "your_user_id" token, err := createBase64Token(appid, appkey, channelID, userID) if err != nil { fmt.Println("Error creating token:", err) return } fmt.Println("Base64 Token:", token) } -
クライアントサイドのトークン生成: 開発中は、サーバーではなくクライアントでトークンを生成できます。計算されたトークンと 5 つのパラメーター (appid、channelid、nonce、userid、timestamp) を含む JSON オブジェクトを構築します。JSON オブジェクトを Base64 エンコードし、ARTC SDK に渡します。
重要この方法では、AppKey やその他の機密情報がアプリケーションに埋め込まれます。テストと開発にのみ使用してください。本番環境ではこの方法を使用しないでください。クライアント側で AppKey を公開すると、深刻なセキュリティリスクが生じ、盗難につながる可能性があります。
-