0
            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;

This is my code. Maybe you can see where I made an error. As for me, API is normal made.

When I run this code it returns an HTTP 400 error code.

1
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;
        }

this is new code to fix error with HMACSHA384

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

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

and some more fixes.

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;
        }

oh and last fix to prove the error

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

You are getting an HTTP 400 error which means the server is (for some reason) refusing to process your request. It could be due to bad string formatting, incorrect or unsuported headers format or MIME type. You might want to take a deeper look at your headers and the way the request in general is being parsed.

Looking at your code, I can only help but notice the "@" at the beginning of your url string declaration. I do not know if this is intented or whether the language you're coding with requires you to use it (I might just be rising a flag where it isn't), I however, and out of pure personal interest (but might help with the issue) would like to know, are you coding C#? .NET? other?

  • 1
    I Use c#, i try many type of headers, 1-st error was with forming hmac 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); – Yura Sultanov Jan 28 '15 at 23:44
  • I see you posted a self-answer, did you happen to fix the whole issue and tackle other bugs? Are you getting a 200 OK now? Can't help much with C#, thought I'd throw some pointers at the least though. – Dhuum Jan 28 '15 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.