• 使用C#完成OKex V5 REST API签名


    本文介绍了OKex V5 REST API签名规则,并使用C#实现了签名。在Demo中使用Flurl.Http类库进行HTTP请求,介绍了使用只读API和交易API获取数据的方法。

    比较遗憾的是,国内网络是无法访问该API的。

    1、签名介绍

    OK-ACCESS-SIGN的请求头是对timestamp + method + requestPath + body字符串(+表示字符串连接),以及SecretKey,使用HMAC SHA256方法加密,通过Base-64编码输出而得到的。
    
    如:sign=CryptoJS.enc.Base64.Stringify(CryptoJS.HmacSHA256(timestamp + 'GET' + '/users/self/verify', SecretKey))
    
    其中,timestamp的值与OK-ACCESS-TIMESTAMP请求头相同,为ISO格式,如2020-12-08T09:08:57.715Z。
    
    method是请求方法,字母全部大写:GET/POST。
    
    requestPath是请求接口路径。如:/api/v5/account/balance
    
    body是指请求主体的字符串,如果请求没有主体(通常为GET请求)则body可省略。如:{"instId":"BTC-USDT","lever":"5","mgnMode":"isolated"}
    
    SecretKey为用户申请APIKey时所生成。如:22582BD0CFF14C41EDBF1AB98506286D

    2、签名实现

    //OKEx V5 API签名
    public static string HmacSha256(string message, string secret)
    {
        var sha256Data = Encoding.UTF8.GetBytes(message);
        var secretData = Encoding.UTF8.GetBytes(secret);
        using (var hmacSha256 = new HMACSHA256(secretData))
        {
            var buffer = hmacSha256.ComputeHash(sha256Data);
            return Convert.ToBase64String(buffer);
        }
    }

    3、异步获取所有产品行情信息

    该API仅需要只读权限。

    //异步获取所有产品行情信息
    public async Task<Tuple<bool, object>> GetOkexTickerssAsync(
        string apiKey, string secretKey, string passPhrase)
    {
        //产品类型
        //SPOT:币币
        //SWAP:永续合约
        //FUTURES:交割合约
        //OPTION:期权
        var api = "/api/v5/market/tickers?instType=SWAP";
    
        //ISO8601 with 3 decimal places
        var timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", CultureInfo.InvariantCulture);
        var signText = $@"{timeStamp}GET{api}";
        var sign = HmacHelper.HmacSha256(signText, secretKey);
    
        var url = $"https://www.okex.com{api}"
            .WithHeader("Content-Type", "application/json")
            .WithHeader("OK-ACCESS-KEY", apiKey)
            .WithHeader("OK-ACCESS-SIGN", sign)
            .WithHeader("OK-ACCESS-TIMESTAMP", timeStamp)
            .WithHeader("OK-ACCESS-PASSPHRASE", passPhrase);
    
        try
        {
            var content = await url.GetStringAsync();
            return new Tuple<bool, object>(true, content);
        }
        catch (Exception e)
        {
            return new Tuple<bool, object>(false, e.Message);
        }
    }

    4、异步获取成交明细(近三个月)

    该API需要交易权限。

    //异步获取成交明细(近三个月)
    public async Task<Tuple<bool, object>> GetOkexFillHistoryAsync(
        string apiKey, string secretKey, string passPhrase)
    {
        //产品类型
        //SPOT:币币
        //MARGIN:币币杠杆
        //SWAP:永续合约
        //FUTURES:交割合约
        //OPTION:期权
        var api = "/api/v5/trade/fills-history?instType=SPOT";
    
        //ISO8601 with 3 decimal places
        var timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", CultureInfo.InvariantCulture);
        var signText = $@"{timeStamp}GET{api}";
        var sign = HmacSha256(signText, secretKey);
    
        var url = $"https://www.okex.com{api}"
            .WithHeader("Content-Type", "application/json")
            .WithHeader("OK-ACCESS-KEY", apiKey)
            .WithHeader("OK-ACCESS-SIGN", sign)
            .WithHeader("OK-ACCESS-TIMESTAMP", timeStamp)
            .WithHeader("OK-ACCESS-PASSPHRASE", passPhrase);
    
        try
        {
            var content = await url.GetStringAsync();
            return new Tuple<bool, object>(true, content);
        }
        catch (Exception e)
        {
            return new Tuple<bool, object>(false, e.Message);
        }
    }
  • 相关阅读:
    Ubuntu 配置IP地址方法
    ubuntu server 16.04安装GPU服务器
    Ubuntu 自动获取ip地址
    Typedef 用法
    linux mount命令详解(iso文件挂载)
    specrate 与specspeed 的区别
    SPEC CPU 使用简介
    编译错误you should not run configure as root (set FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check)
    SPEC CPU 2006编译perl 出错:undefined reference to `pow'
    'gets' undeclared here (not in a function)
  • 原文地址:https://www.cnblogs.com/xhubobo/p/15147158.html
Copyright © 2020-2023  润新知