2015-10-17 67 views
0

我想實現Bittrex API的最新版本的小工具,我做我自己,我已經得到了,除了最沒關係的事情,他們詢問這對在V1.1:PHP到.NET(WinForm的)API的整合會導致一些問題

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的時候,其他一些提示&技巧),但我只是不能得到我的方式應該發送apisign結束....

如果你們中的任何人可以指導我的解決方案,或指示我我正在尋找什麼(因爲我不知道捲曲是什麼),甚至鞭打我一個我可以學習一些示例代碼來理解這個部分,那真是太棒了。

編輯:

我已經更新了上述如下:

// 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:」作爲「:」是非法的性格,我想整合「隨機數」,因爲在編譯所有內容時,由於「隨機數」沒有被引用,認證失敗。所以我試圖將它添加到URL或標題中,但都失敗了。

回答

1

因此,解決辦法其實是「相當」簡單。如果有人在做一些與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")); 
} 
相關問題