认证验证
生成token和secret_key
对任何需要认证的接口,必须生成token
和secret_key
,如果您还没有申请,请前往“个人中心 - 开放接口”页面进行相关操作
认证头
认证头由三部分组成:随机数Nonce、令牌Token、签名Signature
headers = {"Nonce": "1534927978_ab43c", "Token": "tokenndancpwk", "Signature": "304409e2418545095c6c23bc0c0e2aa5d13ac316"}
随机数:是由UTC时区Unix时间戳十进制秒数格式的字符串前10位、'_' 和5位随机字母或数字组成(例如:1534927978_ab43c),请遵循时间误差不能超过60秒,且nonce只能被使用一次。
令牌:是用户申请的token
签名:是由token、secret_key、nonce和params(get或post参数,使用key=value的字符串形式)的 字符串组合,进行顺序排序,对有序的多个字符串拼接,再通过secret_key对数据进行sha1签名,得到返回的字符串。步骤如下:
初始数据 token="tokenndancpwk"
,secret_key="secretwnakwnncwa"
,nonce="1534927978_ab43c"
params={"symbol": "BTC-USDT", "period": "1min", "size": 150}
,params是GET或POST参数 数据排序和拼接 1534927978_ab43cperiod=1minsecretwnakwnncwasize=150symbol=BTC-USDTtokenndancpwk
最后使用sha1计算签名 304409e2418545095c6c23bc0c0e2aa5d13ac316
示例
Python
import hashlibimport randomimport requestsimport timedef _generate_header(token: str, secret_key: str, data: dict) -> dict: """ 生成请求头 :param token: token :param secret_key: secret_key :param data: 参数(GET/POST) :return: 请求头字典 """ nonce = _nonce() return { 'Nonce': nonce, 'Token': token, 'Signature': _sign(token, secret_key, nonce, data) }def _sign(token: str, secret_key: str, nonce: str, data: dict) -> str: """ 生成签名 :param token: token :param secret_key: secret_key :param nonce: 随机数 :param data: 参数(GET/POST) :return: 签名字符串 """ tmp = [token, secret_key, nonce] for d, x in data.items(): tmp.append(str(d) "=" str(x)) return hashlib.sha1(''.join(sorted(tmp)).encode('utf-8')).hexdigest()def _nonce() -> str: """生成随机数""" rs = '_' data = '124567890abcdefghijklmnopqrstuvwxyz' for _ in range(5): rs = random.choice(data) return str(time.time())[:10] rsdef get_demo(): params = None headers = _generate_header(token, secret_key, params) # 当前委托列表 a = requests.get('https://openapi.aofex.com/openApi/entrust/currentList', params=params, headers=headers) print(a.json())def post_demo(): params = {'symbol': 'OT-AQ', 'type': 'buy-limit', 'amount': 100, 'price': 100} headers = _generate_header(token, secret_key, params) # 限价挂单 a = requests.post('https://openapi.aofex.com/openApi/entrust/add', data=params, headers=headers) print(a.json())
JAVA
/** * 发送请求 * * @param args * @return */ private String request(String url, String method, JSONObject data, Map headers) { if (method == null) { method = "GET"; } else { method = method.toUpperCase(); } if (headers == null) { Map emptyHead = new HashMap(); headers = emptyHead; } if (data == null) { JSONObject emptyData = new JSONObject(); data = emptyData; } Map signHeaders = mkHeader(data); Map newheaders = new HashMap(); newheaders.putAll(signHeaders); newheaders.putAll(headers); headers = newheaders; return httpRequest(url, method, data, headers); } /** * 生成随机字符串 * * @return */ private String randomStr() { Random rand = new Random(); char[] letters = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'r', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; String str = ""; int index; boolean[] flags = new boolean[letters.length];// 默认为false for (int i = 0; i
PHP
private function sign($nonce, $data){ $tmpArr = [$this->token, $this->secret_key, $nonce]; foreach($data as $k=>$v){ $tmpArr[] = $k."=".$v; } sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $signature = sha1( $tmpStr ); return $signature;}function getRandChar($length=5){ $str = null; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";//大小写字母以及数字 $max = strlen($strPol)-1; for($i=0;$igetRandChar(); $signHeaders = ["Token"=>$this->token, "nonce"=>$nonce, "Signature"=>$this->sign($nonce, $data)]; return $signHeaders;}
C#
string GetTimeStamp(){ long time = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; return time.ToString();} string RandomStr() { string str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; StringBuilder SB = new StringBuilder(); Random rd = new Random(); for (int i = 0; i
Nodejs
class AApi{ constructor(token,secret_key){ this._token=token; this._secret_key=secret_key; this._BASE_URL = 'https://openapi.aofex.com' } getHeader(){ var nonce = Math.round(Date.now() / 1000) "_" this.getNumChar().toString(); var headers = { 'content-type': 'application/json', 'Accept': 'application/json', 'Token' : this._token, 'Nonce' : nonce, 'Signature' : this.sign(nonce, data) } return headers; } getNumChar() { var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var n = 5, s = ""; for(var i = 0; i
本文地址:http://bilianwu.com/78722.html
版权声明:项目均采集于互联网, 空投币 无法审核全面,且希望大家能赚钱,请谨慎切勿上当受骗!
温馨提示:★★★天上真会掉馅饼!天道酬勤,都是机会!不错过每个空投糖果!真假难以辨认,尽量0撸!
版权声明:项目均采集于互联网, 空投币 无法审核全面,且希望大家能赚钱,请谨慎切勿上当受骗!
温馨提示:★★★天上真会掉馅饼!天道酬勤,都是机会!不错过每个空投糖果!真假难以辨认,尽量0撸!