Api

Bitfinex 400 錯誤

  • January 28, 2015
           string url = @"https://api.bitfinex.com/v1/balances";
           string paramDict = "{\"request\": \"/v1/balances\",\"nonce\": \"" + GetNonce() * 10000 + "\"}";
           string payload = Convert.ToBase64String(Encoding.ASCII.GetBytes(paramDict));
           string signature = BitConverter.ToString(hmac.ComputeHash(Encoding.ASCII.GetBytes(payload))).Replace("-","").ToLower();

           var headers = new Dictionary<string, string>
           {
               {"X-BFX-APIKEY",key},
               {"X-BFX-PAYLOAD",payload},
               {"X-BFX-SIGNATURE",signature}
           };


           var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
           if (request == null)
               throw new Exception("Non HTTP WebRequest");

           var data = System.Text.Encoding.ASCII.GetBytes(paramDict);
           request.Method = "POST";
           request.Timeout = 30000;
           request.ContentLength = data.Length;
           foreach (var a in headers)
           {
               request.Headers.Add(a.Key, a.Value);
           }

           var write = request.GetRequestStream();
           write.Write(data, 0, data.Length);

           var respone = request.GetResponse() as HttpWebResponse;

這是我的程式碼。也許你可以看到我在哪裡犯了錯誤。至於我,API是正常的。

當我執行此程式碼時,它會返回 HTTP 400 錯誤程式碼。

string Query()
       {
           string url = "https://api.bitfinex.com/v1/balances";
           string paramDict = "{\"request\":\"\\/v1\\/balances\",\"nonce\":\"" + GetNonce() * 100000 + "\"}";
           string payload = EncodeTo64(paramDict);
           string message = payload;
           string skey = secret;
           System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
           byte[] keyByte = encoding.GetBytes(skey);
           HMACSHA384 hmacsha384 = new HMACSHA384(keyByte);
           byte[] messageBytes = encoding.GetBytes(message);
           byte[] hashmessage = hmacsha384.ComputeHash(messageBytes);
           string signature = this.ByteToString(hashmessage);
           var headers = new Dictionary<string, string>
           {
               {"X-BFX-APIKEY",key},
               {"X-BFX-PAYLOAD",payload},
               {"X-BFX-SIGNATURE",signature}
           };
           var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
           if (request == null)
               throw new Exception("Non HTTP WebRequest");

           var data = encoding.GetBytes(paramDict);
           request.Method = "POST";
           request.Timeout = 30000;
           request.ContentLength = data.Length;
           foreach (var a in headers)
           {
               request.Headers.Add(a.Key, a.Value);
           }

           var write = request.GetRequestStream();
           write.Write(data, 0, data.Length);

           var respone = request.GetResponse() as HttpWebResponse;

           return null;
       }

這是修復 HMACSHA384 錯誤的新程式碼

string ByteToString(byte[] buff)
       {
           string sbinary = "";

           for (int i = 0; i < buff.Length; i++)
           {
               sbinary += buff[i].ToString("X2"); // hex format
           }
           return (sbinary);
       }

和一些更多的修復。

long GetNonce()
       {
           return nonce++;
       }

       static public string EncodeTo64(string toEncode)
       {
           byte[] toEncodeAsBytes
                 = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
           string returnValue
                 = System.Convert.ToBase64String(toEncodeAsBytes);
           return returnValue;
       }

哦,最後修復以證明錯誤

   string signature = this.ByteToString(hashmessage).ToLower();

您收到HTTP 400 錯誤,這意味著伺服器(出於某種原因)拒絕處理您的請求。這可能是由於錯誤的字元串格式、不正確或不受支持的標頭格式或MIME 類型。您可能想更深入地了解您的標頭以及解析請求的一般方式。

查看您的程式碼,我只能注意到您的 url 字元串聲明開頭的“@”。我不知道這是否是有意的,或者您正在編碼的語言是否需要您使用它(我可能只是在升旗,而不是),但是出於純粹的個人興趣(但可能會有所幫助有問題)想知道,你在編寫 C# 程式碼嗎?。網?其他?

引用自:https://bitcoin.stackexchange.com/questions/35779