PHP 到 .NET (winform) API 集成导致一些问题

PHP to .NET (winform) API integration cause some problems

我正在尝试将最新版本的 Bittrex api 实现到我自己做的一个小工具中,除了他们为 V1.1 询问这个之外,我的大部分事情都还不错:

    For this version, we use a standard HMAC-SHA512 signing. Append apikey and nonce to your request and calculate the HMAC hash and include it under an apisign header. Note: the nonce is not respected right now but will be enforced later.

$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);

为了实现它,我这样做了:

// TODO : add a way to get this from the user
var apiKey = ApiKey;
var apiSecret = ApiSecret;

// var nonce = (int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; // same as time() in PHP
var encoding = Encoding.UTF8;
var url = @"https://bittrex.com/api/v1.1/account/getbalances?apikey=" + apiKey;
var result = Gethmacsha512(encoding, apiSecret, url);

// some var for the request
var account = new List<AccountCurrencies>();

// sending it to get the response
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("apisign:",result);
request.ContentType = "application/json";
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();

Resp.GetValue(stream, account);
return account;

哪个 returns 我觉得 HTTPHEADER 不好。

如您所见,我使用了我在该网站上找到的解决方案(Gethmacsha512,将时间从 php 获取到 .Net 的方法,以及其他一些提示和技巧),但我无法获得我应该发送 api签名的方式....

如果你们中的任何人可以指导我找到解决方案,或者告诉我我正在寻找什么(因为我不知道 curl 是什么),或者甚至给我一些我可以研究的示例代码理解那部分,那就太棒了。

编辑:

我已经更新了上面的内容如下:

// TODO : add a way to get this from the user
var apiKey = ApiKey;
var apiSecret = ApiSecret;

var nonce = (int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; // same as time() in PHP
var encoding = Encoding.UTF8;
var url = @"https://bittrex.com/api/v1.1/account/getbalances?apikey=" + apiKey;
            var urlForAuth = url + "&nonce=" + nonce;
            var result = Gethmacsha512(encoding, apiSecret, urlForAuth);

// some var for the request
var account = new List<AccountCurrencies>();

// sending it to get the response
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("apisign",result);
request.Headers.Add("nonce", nonce.ToString());
request.ContentType = "application/json";
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();

Resp.GetValue(stream, account);
return account;

上述问题的罪魁祸首是 "apisign:",因为 ':' 是非法字符,我尝试整合 "nonce" 因为虽然一切都编译,但由于 "nonce" 未提交。所以我尝试将其添加到 URL 或 header 中,但都失败了。

所以解决方案实际上 "rather" 很简单。如果有人也在用 Bittrex API 做一些事情,这里是有效的代码:

public static List<AccountCurrencies> GetAccountCurrencies()
    {
        if (Settings.Default.APIKey == null || Settings.Default.APISecret == null) return new List<AccountCurrencies>();
        var nonce = (int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; // same as time() in PHP, need to integrate it
        var encoding = Encoding.UTF8;
        var urlForAuth = @"https://bittrex.com/api/v1.1/account/getbalances?apikey=" + Settings.Default.APIKey + "&nonce=" + nonce;
        var result = Gethmacsha512(encoding, Settings.Default.APISecret, urlForAuth);

    // some var for the request
    var account = new List<AccountCurrencies>();

    // sending it to get the response
    var request = (HttpWebRequest)WebRequest.Create(urlForAuth);
    request.Headers.Add("apisign",result);
    //request.Headers.Add("nonce", nonce.ToString());
    request.ContentType = "application/json";
    var response = (HttpWebResponse)request.GetResponse();
    var stream = response.GetResponseStream();

    Resp.GetValue(stream, account);
    return account;
}


private static string Gethmacsha512(Encoding encoding, string apiSecret, string url)
{
    // doing the encoding
    var keyByte = encoding.GetBytes(apiSecret);
    string result;
    using (var hmacsha512 = new HMACSHA512(keyByte))
    {
        hmacsha512.ComputeHash(encoding.GetBytes(url));

        result = ByteToString(hmacsha512.Hash);
    }
    return result;
}

static string ByteToString(IEnumerable<byte> buff)
{
    return buff.Aggregate("", (current, t) => current + t.ToString("X2"));
}