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;

返回我,HTTPHEADER是不好的。

正如你所看到的,我使用了我在这个网站(Gethmacsha512)上找到的解决方案,从php到。net获得时间的方法,以及其他一些技巧&但我就是不明白我应该通过....发送消息的方式

如果你们中的任何一个人能给我一个解决方案,或者告诉我我在寻找什么(因为我不知道卷曲是什么),或者甚至鞭打我一点示例代码,我可以学习来理解这部分,那将是非常棒的。

编辑:

我将以上内容更新如下:

// 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中,或者在标题中,但都失败了。

所以解决方案实际上"相当"简单。如果有人也在用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"));
}