获取 Access Token 1 基础概念 access_token 是接口的全局唯一票据, 接入方调用各接口时都需使用 access_token 开发者需要妥善保存,access_token 的存储至少要保留 512 个字符空间 access_token 的有效期目前为 2 个小时, 需定时刷新 接入方可以使用 app_id 和 server_app_secret 调用本接口来获取 access_token 请注意 : 1. app_id 和 server_app_secret 需要联系 ZEGO 技术支持获得 2. 调用所有接口时均需使用 https 协议 2 请求说明 http 请求方式 : POST/JSON, 需使用 https 服 务 调用频率限
环境国内正式环境海外正式环境国内测试环境海外测试环境 制 1 次 / 秒 1 次 / 秒 1 次 / 秒 1 次 / 秒 请求示例 curl -X POST https://liveroom{appid- api.zego.im/cgi/token (https://liveroom{appid- api.zego.im/cgi/token) -d 'json_str' api.zegocloud.com/cgi/token curl -X POST https://liveroom{appid- (https://liveroom{appid- api.zegocloud.com/cgi/token) -d 'json_str' curl -X POST https://test2-liveroomapi.zego.im/cgi/token (https://test2-liveroomapi.zego.im/cgi/token) -d 'json_str' curl -X POST https://test2-liveroomapi.zegocloud.com/cgi/token (https://test2- liveroom-api.zegocloud.com/cgi/token) -d 'json_str' json_str 内容如下 :
{ "version": 1, "seq": 1, "app_id": 11, "biz_type": 0, "token": 'TOKEN', 3 参数说明 是否 参数名 类型 必须 说明 version Int 是 协议版本号, 默认填 1 seq Int 是 协议序列号, 每次必须请求唯一, 每次请求时步进 1 递增 app_id Int 是 第三方用户唯一凭证 biz_type Int 否 App 业务类型 ;0:live,2:rtv ( 可以不填, 默认为 0) token String 是 即构后台服务认证凭证 token 生成规则说明 :
tokeninfo = '{ "ver": 1, "hash": "adfdkjakka1213aa", "nonce": "1934892311", "expired": 1531393997, // 单位 : 秒 ' 注意这里要先构造 JSON, 再转成字符串 token = base64(tokeninfo) token 字段说明 : 参数名 类型 说明 ver Int 版本号 ; 默认填 1 hash String hash = md5sum(sprintf("%u%s%s%u",app_id,server_ md5sum 最终结果为 32 字节小写 hex 编码 nonce String 随机串,16 bytes expired Int token 失效时间,Unix 时间戳, 单位 : 秒 token 中的 hash 字段生成规则说明 : 参数名 类型 说明 app_id Int App 唯一标识 server_app_secret String server 接口 App 唯一凭证密钥,16 bytes, 请联系技术支持获取
4 返回说明 正常情况下, 会返回下述 JSON 数据包给调用方 : { "code": 0, "data": { "access_token": "c205dytwqtd4zlzjsjbltxy0v3fzyutby1q9zeo0egfjcet 3WVBKSlFGeXh5aGZITjJWYnlBcT0", "expires_in": 7200, "message": "success" 返回参数说明 : 参数名 类型 说明 code Int 返回码 message String 操作结果描述 access_token String 获取到的凭证 expires_in Int 凭证有效时间, 单位 : 秒 错误时会返回错误码等信息,JSON 数据包示例如下 ( 该示例为 server_app_secret 无效错误 ):
{ "code": 40005, "message": "appsecret 错误 " 5 获取 token 示例代码 5.1 Java 示例代码 package demo; import java.io.unsupportedencodingexception; import java.security.*; import java.util.*; import com.alibaba.fastjson.*; public class tokentest { public static void main(string[] args) throws UnsupportedEncodingException { long current_time = System.currentTimeMillis(); // 获取当前 unix 时间戳 long expired_time = current_time+7200; // 过期 unix 时间戳, 单位 : 秒 String appid = 000000000; // Int 类型, 这里填写各自分配的 appid String serversecret = "00000000000000000000000000"; // 这里填写对应的 se rver_app_secret String nonce = "xxxxxxxxx"; // 自定义的随机数 //System.out.println(current_time); //System.out.println(expired_time); // 待加密信息 String originstring = appid + serversecret + nonce + Long.toString(expir ed_time); // 待加密的信息 //System.out.println(originString);
//hash 加密 String hashstring = getmd5(originstring); // 加密后的 hash System.out.println("--hashString--:"+hashString); // 定义一个 tokeninfo json LinkedHashMap hashmap = new LinkedHashMap(); hashmap.put("ver",1); hashmap.put("hash",hashstring); hashmap.put("nonce",nonce); hashmap.put("expired",expired_time); String tokeninfo= JSON.toJSONString(hashMap); //System.out.println("----------hashMap--------"+hashMap); //System.out.println("----------111--------"+tokeninfo); //Base64 加密 final Base64.Encoder encoder = Base64.getEncoder(); //final Base64.Decoder decoder = Base64.getDecoder(); final byte[] textbyte = tokeninfo.getbytes("utf-8"); // 加密 // 解密 final String encodedtext = encoder.encodetostring(textbyte); System.out.println("--token--:"+encodedText); // 生成 MD5 public static String getmd5(string message) { String md5 = ""; try { MessageDigest md = MessageDigest.getInstance("MD5"); // 创建一个 md5 算法对象 byte[] messagebyte = message.getbytes("utf-8"); byte[] md5byte = md.digest(messagebyte); // 获得 MD5 字节数组,16*8=128 位 md5 = bytestohex(md5byte); // 转换为 16 进制字符串 catch (Exception e) { System.out.println("erro md5 creat!!!!"); e.printstacktrace(); return md5;
// 二进制转十六进制 public static String bytestohex(byte[] bytes) { StringBuffer hexstr = new StringBuffer(); int num; for (int i = 0; i < bytes.length; i++) { num = bytes[i]; if(num < 0) { num += 256; if(num < 16){ hexstr.append("0"); hexstr.append(integer.tohexstring(num)); return hexstr.tostring(); /** * 最后, 将生成的 Token 复制到以下 curl -X POST 替换掉其中的 token, 修改对应的 app_id, 然后取请求得到正确的 "access_token" * * */ // curl -X POST https://liveroom{app_id-api.zego.im/cgi/token -d '{"version": 1,"seq": 1,"app_id": 000000000,"biz_type": 0,"token": token' 5.2 php 示例代码
<?php $current_time = time(); // 获取当前时间 $expired_time = $current_time + 7200; // 过期时间, 单位 : 秒 $app_id = 000000000; // Int 类型, 这里填写各自分配的 appid $server_app_secret = "00000000000000000000000000"; // 这里填写对应的 server_app_s ecret $nonce = "xxxxxxxxx"; // 自定义的随机数 // 待加密信息 $origin = $app_id. $server_app_secret. $nonce. $expired_time; //hash 加密 $hash = md5($origin); $token = [ 'ver' => 1, 'hash' => $hash, 'nonce' => $nonce, 'expired' => $expired_time ]; // 定义一个 tokeninfo json $token = json_encode($token); //Base64 加密 $encode_token = base64_encode($token); echo $encode_token; /** * 最后, 将生成的 Token 复制到以下 curl -X POST 替换掉其中的 token, 修改对应的 app_id, 然后取请求得到正确的 "access_token" * * */ // curl -X POST https://liveroom{app_id-api.zego.im/cgi/token -d '{"version": 1,"seq": 1,"app_id": 000000000,"biz_type": 0,"token": token'
5.3 python 示例代码 import time import hashlib import base64 import json def buildtoken(): current_time = int(time.time()) # 当前 unix 时间戳 expired_time = int(current_time + 7200) # 过期 unix 时间戳, 单位 : 秒 print(" 当前时间 : " + str(int(current_time))) print(" 过期时间 : " + str(int(expired_time))) appid = '123456789' # 分配给客户的 appid serversecret = '12345678123456781234567812345678' # 这里填写对应的 server_app _secret nonce = '1234567812345678' # 建议使用随机串 # 待加密信息 astr = appid + serversecret + nonce + str(expired_time) print(" 未 hash 的串 : " + astr) # hash 加密 md5obj = hashlib.md5() md5obj.update(astr.encode(encoding='utf-8')) # hashstr 后的字符串 hashstr = md5obj.hexdigest() print("hashstr 后的字符串 : " + hashstr) # tokeninfo tokeninfo = {'ver':1,'hash':hashstr,"nonce":nonce,"expired":expired_time tokeninfo = json.dumps(tokeninfo) token = base64.b64encode(bytes(tokeninfo,'ascii')) print(token) return token if name == ' main ': buildtoken()
# # 最后, 将生成的 Token 复制到以下 curl -X POST 替换掉其中的 token, 修改对应的 app_id, 然后取请求得到正确的 "access_token" # # curl -X POST https://liveroom{app_id-api.zego.im/cgi/token -d '{"version": 1, "seq": 1,"app_id": 000000000,"biz_type": 0,"token": token'