手动生成token_生成 Token

本文详细介绍了如何生成Token,包括获取AppSecret、生成临时和正式Token的流程,以及不同编程语言(PHP、Python、Java、Node.js)的示例代码。Token在安全验证中起到重要作用,确保了应用在不同阶段的鉴权需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 生成 Token

Token 是一种动态密钥,通过 AppKey、AppSecret、用户名、有效时间戳等参数生成,安全性较高。在正式生产环境等对安全要求较高的场景中,我们推荐使用 Token 鉴权。

TIP

在生成 Token 前请确保您已经在控制台开启了 Token 鉴权模式。具体参考 开启 Token 鉴权模式 。

在生成 Token 前,您需要在控制台获取必要的参数:AppSecret 。具体参考 获取 AppSecret 。

在项目集成测试阶段,可使用由菊风平台提供的临时 Token 进行测试,有效期为 24 小时。具体参考 生成临时 Token 。

在项目准备正式上线阶段,开发者需要在自己的服务端部署 Token 服务来生成正式 Token。具体参考 生成正式 Token 。

在生成自己的 Token 后您需要在集成过程中将 Token 作为 password 参数传入 login(opens new window) 。具体参考 在代码中使用 Token 。

# 获取 AppSecret

AppSecret 是生成生成 Token 的必要参数。通过下列步骤获取AppSecret: 或者手动访问该界面: 进入控制台 - 服务管理 - 应用管理

在 操作 一栏中点击 设置

图标

在 状态配置 中 点击 AppSecret 后的 添加

按钮。

TIP

每个应⽤最多⽣成两个 AppSecret。

# 生成 Token

# 生成临时 Token

为了方便测试,菊风提供临时 Token 的功能。您可以在菊风云控制台获取临时 Token ,具体步骤如下: 或者手动访问该界面: 进入控制台 - 服务管理 - 应用管理

在 操作 一栏中点击 设置

图标

在 状态配置 中 点击 临时 Token 后的 添加

按钮。

输入AccountID,然后点击生成临时Token。

WARNING 临时 Token 仅作为演示和测试用途。在生产环境中,需要自行部署服务器生成 Token 。

临时 Token 的有效期为 24 小时,用于在项目测试阶段进行鉴权。

# 生成正式 Token

正式 Token 将用于正式上线阶段,生成参数包括 AppKey,AppSecret,用户名,有效期等。

在项目准备正式上线阶段,开发者需要在自己的服务端部署 Token 服务,并自行生成 Token。

# 必要的参数

在生成 Token 之前,需要准备以下参数: 参数 描述 备注 AppKey AppKey 是应用在 菊风云平台 中的唯一标识,类似应用的身份证。通过在平台创建应用获取。 通过创建应用获取

AppSecret AppSecret 为长度 20 的随机字符串,数字和小写字母生成的 AppSecret 保存在对应数据库的 App 属性中

AccountID 用户名,在 SDK 集成中使用的 userID 自定义

Salt 随机生成的数,范围 1-254 自定义

Timestamp 过期时间的UNIX时间戳,默认是当前时间 + 24小时 自定义

# 示例代码

生成 Token 的伪代码如下:

Ver = '01'

AppKey =

AppSecret =

AccountID =

Salt =

Timestamp =

base64 =

byte1 =

byte4 =

strx =

hmac-sha256 =

substr =

Payload = base64( byte1(Salt) + byte4(xor(Timestamp, Salt)) + byte4(xor(crc32(AppKey), Salt)) + strx(AccountID, Salt) )

Signature = base64( hmac-sha256(AppSecret, Payload) )

AppSecretDigest = substr(AppSecret, 0, 6)

Token = Ver + AppSecretDigest + Signature + PayloadPHP 生成 Token:

class CreateToken{

private function byte1w($val,$salt = 0){

$ds = pack('C', intval($val) ^ $salt);

return $ds;

}

private function byte4w($val, $salt = 0){

$salt32 = ($salt << 24) + ($salt << 16) + ($salt << 8) + $salt;

$dfrw = pack('N', intval($val ^ $salt32));

return $dfrw;

}

private function xstrw($val, $salt){

$ret = $this->byte1w(strlen($val), $salt);

$strlen = strlen($val);

for ($i=0; $i < $strlen ; $i++) {

$ret .= $this->byte1w(($salt ^ ord($val[$i])) & 0xff);

}

return $ret;

}

/**

* @param $AppKey

* @param $AppSecret 密钥

* @param $AccountId

* @param ...

* @return token

*/

public function edit_build($Ver,$AppKey,$AppSecret,$AccountId,$Salt,$Timestamp){

//加密过程

$first = $this->byte1w($Salt);

$second = $this->byte4w($Timestamp, $Salt);

$third = $this->byte4w(crc32($AppKey) & 0xffffffff, $Salt);

$fourth = $this->xstrw($AccountId, $Salt);

//组合生成

$Payload = base64_encode($first . $second . $third . $fourth);

$AppSecretDigest = substr($AppSecret, 0, 6);

$digest = hash_hmac("sha256",$Payload, $AppSecret,true);

$Signature = base64_encode($digest);

return $Ver . $AppSecretDigest . $Signature . $Payload;

}

}

$Salt = mt_rand(1,254); //盐1~254随机整数

$Timestamp = time() + 24 * 3600;//过期时间 默认当前时间戳+有效期 当前是1天 单位到秒

$ver = '01'; //版本号默认

$AppKey = '';//Appkey 在控制台服务管理->应用管理->安全信息处可见

$AppSecret = '';//应用密钥

$AccountId = 'test1'; //登录用户名

$PCreateToken = new CreateToken;

$Token = $PCreateToken->edit_build($ver,$Appkey,$AppSecret,$AccountId,$Salt,$Timestamp);

echo $Token;

?> python 生成 Token:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

import time

import hmac

import base64

import random

import struct

from zlib import crc32

from hashlib import sha256

def byte1w(val, salt = 0):

return struct.pack('>B', int(val) ^ salt)

def byte1r(val, salt = 0):

ret, = struct.unpack('>B', val)

return ret ^ salt

def byte2w(val, salt = 0):

salt16 = (salt << 8) + salt

return struct.pack('>H', int(val) ^ salt16)

def byte2r(val, salt = 0):

ret, = struct.unpack('>H', val)

return ret ^ ((salt << 8) + salt)

def byte4w(val, salt = 0):

salt32 = (salt << 24) + (salt << 16) + (salt << 8) + salt

return struct.pack('>I', int(val ^ salt32))

def byte4r(val, salt = 0):

ret, = struct.unpack('>I', val)

return ret ^ ((salt << 24) + (salt << 16) + (salt << 8) + salt)

def xstrw(val, salt = 0):

ret = byte1w(len(val), salt)

for i in range(len(val)):

ret += byte1w((salt ^ ord(val[i])) & 0xff)

return ret

def xstrr(val, salt):

cnt = byte1r(val[0:1], salt)

ret = ''

for i in range(cnt):

ret += chr((salt ^ byte1r(val[i+1:i+2])) & 0xff)

return ret

class AuthToken:

def __init__(self, AppKey="", AppSecret="", AccountId=""):

random.seed(time.time())

self.Ver = '01'

self.AppKey = AppKey

self.AppSecret = AppSecret

self.AccountId = AccountId

self.Salt = random.randint(1, 254)

self.Timestamp = int(time.time()) + 24 * 3600

self.Error = ""

def build(self):

if len(self.AppSecret) <= 6:

raise ValueError('Invalid AppSecret')

if len(self.AccountId) == 0:

raise ValueError('No AccountID')

if len(self.AccountId) > 128:

raise ValueError('AccountID Too Long')

Payload = base64.b64encode(byte1w(self.Salt) + byte4w(self.Timestamp, self.Salt) + byte4w(crc32(self.AppKey) & 0xffffffff, self.Salt) + xstrw(self.AccountId, self.Salt))

AppSecretDigest = self.AppSecret[0:6]

Signature = base64.b64encode(hmac.new(self.AppSecret, Payload, sha256).digest())

return self.Ver + AppSecretDigest + Signature + Payload

def main():

at = AuthToken(AppKey="******************", AppSecret="******************", AccountId="******************")

token = at.build()

print(token)

if __name__ == "__main__":

main() python3 生成 Token:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

import time

import hmac

import base64

import random

import struct

from binascii import crc32

from hashlib import sha256

def byte1w(val, salt=0):

return struct.pack('>B', int(val) ^ salt)

def byte1r(val, salt=0):

ret, = struct.unpack('>B', val)

return ret ^ salt

def byte2w(val, salt=0):

salt16 = (salt << 8) + salt

return struct.pack('>H', int(val) ^ salt16)

def byte2r(val, salt=0):

ret, = struct.unpack('>H', val)

return ret ^ ((salt << 8) + salt)

def byte4w(val, salt=0):

salt32 = (salt << 24) + (salt << 16) + (salt << 8) + salt

return struct.pack('>I', int(val ^ salt32))

def byte4r(val, salt=0):

ret, = struct.unpack('>I', val)

return ret ^ ((salt << 24) + (salt << 16) + (salt << 8) + salt)

def xstrw(val, salt=0):

ret = byte1w(len(val), salt)

for i in range(len(val)):

ret += byte1w((salt ^ ord(val[i])) & 0xff)

return ret

def xstrr(val, salt):

cnt = byte1r(val[0:1], salt)

ret = ''

for i in range(cnt):

ret += chr((salt ^ byte1r(val[i + 1:i + 2])) & 0xff)

return ret

class AuthToken:

def __init__(self, AppKey="", AppSecret="", AccountId=""):

random.seed(time.time())

self.Ver = '01'

self.AppKey = AppKey

self.AppSecret = AppSecret

self.AccountId = AccountId

self.Salt = random.randint(1, 254)

self.Timestamp = int(time.time()) + 24 * 3600

self.Error = ""

def build(self):

if len(self.AppSecret) <= 6:

raise ValueError('Invalid AppSecret')

if len(self.AccountId) == 0:

raise ValueError('No AccountID')

if len(self.AccountId) > 128:

raise ValueError('AccountID Too Long')

Payload = base64.b64encode(byte1w(self.Salt) + byte4w(self.Timestamp, self.Salt) + byte4w(crc32(self.AppKey.encode()) & 0xffffffff,self.Salt) + xstrw(self.AccountId,self.Salt))

AppSecretDigest = self.AppSecret[0:6]

Signature = base64.b64encode(hmac.new(bytes(self.AppSecret.encode('utf-8')), Payload, sha256).digest())

Signature_str = bytes.decode(Signature)

Payload_str = bytes.decode(Payload)

return self.Ver + AppSecretDigest + Signature_str + Payload_str

def main():

at = AuthToken(AppKey="*************", AppSecret ="*************", AccountId ="*************")

token = at.build()

print(token)

if __name__ == "__main__":

main() Java 生成 Token

import java.nio.ByteBuffer;

import java.nio.charset.StandardCharsets;

import java.util.zip.CRC32;

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

import java.util.Random;

public class CreateToken {

public static void main(String []args) {

String ver = "01"; //版本号默认

String appkey = ""; //Appkey 在控制台服务管理->应用管理->安全信息处可见

String appSecret = "1233456789"; //应用密钥

String accountId = "test1"; //登录用户名

Random random = new Random();

byte salt = (byte) (random.nextInt(254) + 1); //盐1~254随机整数

int timestamp = (int) (System.currentTimeMillis() / 1000) + 24 * 3600; //过期时间 默认当前时间戳+有效期 当前是1天 单位到秒

try {

BASE64Encoder encoder = new BASE64Encoder();

byte first = byte1(salt, (byte) 0);

byte[] second = byte4(timestamp, salt);

CRC32 crc32 = new CRC32();

crc32.update(appkey.getBytes());

byte[] third = byte4((int) crc32.getValue(), salt);

byte[] forth = strx(accountId, salt);

ByteBuffer byteBuffer = ByteBuffer.allocate(1 + second.length + third.length + forth.length);

byteBuffer.put(first).put(second).put(third).put(forth);

String payload = encoder.encode(byteBuffer.array());

String signature = generateSignature(appSecret, payload);

String appSecretDigest = appSecret.substring(0, 6);

String token = ver + appSecretDigest + signature + payload;

System.out.println(token);

} catch (Exception e) {

System.out.println("BASE64加解密异常");

e.printStackTrace();

}

}

private static String generateSignature(String secret, String content) {

try {

BASE64Encoder encoder = new BASE64Encoder();

Mac sha256_HMAC = Mac.getInstance("HmacSHA256");

SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256");

sha256_HMAC.init(secret_key);

return encoder.encode(sha256_HMAC.doFinal(content.getBytes(StandardCharsets.UTF_8)));

} catch (Exception e) {

System.out.println(e.getMessage());

}

return "";

}

private static byte byte1(byte input, byte salt) {

return (byte) (input ^ salt);

}

private static byte[] byte4(int input, byte salt) {

byte[] ret = new byte[4];

ret[0] = byte1((byte) (input >> 24), salt);

ret[1] = byte1((byte) (input >> 16), salt);

ret[2] = byte1((byte) (input >> 8), salt);

ret[3] = byte1((byte) (input), salt);

return ret;

}

private static byte[] strx(String input, byte salt) {

byte[] ret = new byte[input.length() + 1];

ret[0] = byte1((byte) input.length(), salt);

for (int i = 0; i < input.length(); i++) {

ret[i + 1] = byte1(input.getBytes()[i], salt);

}

return ret;

}

} NodeJS 生成 Token

var CryptoJS = require('crypto-js');

var Long = require('long');

var crc32 = require('crc-32');

var UINT32 = require('cuint').UINT32;

var jspack = require('bufferpack');

var ord = function(str) {

var n = str.charCodeAt(0);

return n;

}

function randomNum(minNum,maxNum){

switch(arguments.length){

case 1:

return parseInt(Math.random()*minNum+1,10);

break;

case 2:

return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);

break;

default:

return 0;

break;

}

}

//###########################################################

function byte1w(val, salt = 0){

var num = val ^ salt;

var ds = jspack.pack('B', [num]);

return ds;

}

function byte4w(val, salt = 0){

var fd = (salt << 24);

if(fd){

fd = (salt << 24 >>> 0);

}

var salt32 = fd + (salt << 16) + (salt << 8) + salt;

var numa = val ^ salt32;

if(numa<0){

var num = numa >>> 0;

}else{

var num = numa;

}

var Abbuf = new Uint32Array(4);

Abbuf[0]= 0xFF;

Abbuf[1]= 0xFF;

Abbuf[2]= 0xFF;

Abbuf[3]= 0xFF;

var buf = Buffer.from(Abbuf);

buf.writeUInt32BE(num);

return buf;

}

function xstrw(val, salt){

var passArr = val.split('');

var ret = byte1w(passArr.length, salt);

var ret_buf_len = ret.length;

var ret_arr = new Array();

ret_arr.push(ret);

for (var i = 0; i < passArr.length; i++) {

retz = byte1w((salt ^ ord(passArr[i])) & 0xff);

ret_buf_len += retz.length;

ret_arr.push(retz);

}

var ret_all = Buffer.concat(ret_arr, ret_buf_len);

return ret_all;

}

function pack_64(fd,B4,sd64,dref){

pack_buf_len = fd.length+B4.length+sd64.length+dref.length;

var pack_arr = new Array();

pack_arr.push(fd);

pack_arr.push(B4);

pack_arr.push(sd64);

pack_arr.push(dref);

var pack_all = Buffer.concat(pack_arr, pack_buf_len);

return pack_all.toString('base64');

}

var Ver = '01';

var AppKey = '*************';

var AppSecret = '*************';

var AccountId = '*************';

var Salt = randomNum(1,254);//102;

var Timestamp = Math.floor(new Date() / 1000) + (24 * 3600);//1607857680;

//###################################################

var fd = byte1w(Salt);

var B4 = byte4w(Timestamp,Salt);

var sddf = UINT32(crc32.str(AppKey)).and(UINT32(0xffffffff)).toNumber();

var sd64 = byte4w(sddf,Salt);

var dref = xstrw(AccountId, Salt);

var Payload = pack_64(fd,B4,sd64,dref);

//####################################################

AppSecretDigest = AppSecret.substring(0,6);

var sds = CryptoJS.HmacSHA256(Payload,AppSecret);

var Signature = CryptoJS.enc.Base64.stringify(sds);

var token = Ver + AppSecretDigest + Signature + Payload;

console.log("token:",token);

# 使用 Token

在生成 Token 后,需要在集成 【发起登录】 功能时将 Token 作为password 参数传入 login 。以 Java 代码为例:

JCClient.LoginParam loginParam = new JCClient.LoginParam();

// 发起登录

mClient.login("userID", "Token","serverAddress", loginParam);

更多登录相关的集成文档请访问:一对一语音通话(或其他产品) -> 基本功能集成 -> 登录 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值