BTCMarkets / API

API
119 stars 30 forks source link

fundtransfer api throws 'Authentication failed' #149

Closed muralispm closed 6 years ago

muralispm commented 6 years ago

Hi, I am able to get data from /market/BTC/AUD/tick API, but I got 'Authentication failed' error while calling /fundtransfer/history api. I used the same code to generate signature for both the calls. Any clue on what could be the issue?

Here is my code in C#: string url = "https://api.btcmarkets.net/fundtransfer/history"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

 long time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
        string secret = "<My secret>";          
        byte[] base64Secret = Convert.FromBase64String(secret);
        string toSign = url + '\n' + time.ToString() + '\n';
        if(!string.IsNullOrWhiteSpace(payload))
        {
            toSign += payload;
        }

        byte[] toSignByte = Encoding.UTF8.GetBytes(toSign);
            string signature = "";
        using (HMACSHA512 hmac = new HMACSHA512(base64Secret))
        {
            byte[] signatureBytes = hmac.ComputeHash(toSignByte);
            signature = Convert.ToBase64String(signatureBytes);
        }
            string timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
            httpWebRequest.Accept = "application/json";                
            httpWebRequest.Headers.Add("Accept-Charset", "UTF-8");
            httpWebRequest.ContentType = "application/json";                
            httpWebRequest.Headers.Add("apikey", "<my api key>");
            httpWebRequest.Headers.Add("timestamp", timestamp );
            httpWebRequest.Headers.Add("signature", signature);

            if (!string.IsNullOrEmpty(payload))
            {
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(payload);
                    streamWriter.Flush();
                }
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            string str = JsonConvert.SerializeObject(httpResponse);
            string res = string.Empty;
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                res = streamReader.ReadToEnd();

            }
            Console.WriteLine(res.ToString());

Regards, Murali.

justin-ngin commented 6 years ago

Hi @muralispm ,

Based on your code, this issue usually arises from calculating the timestamp twice. Please try converting the original timestamp that you used for the signature to a string for use in the header.

Cheers, Justin

muralispm commented 6 years ago

Hi Justin, Thanks for the reply. I changed the code as you said but still I got the Authentication Failed message.

Here is my updated code:

    string timeStamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
    string secret = "<My secret>";          
    byte[] base64Secret = Convert.FromBase64String(secret);
    string toSign = url + '\n' + timeStamp + '\n';
    if(!string.IsNullOrWhiteSpace(payload))
    {
        toSign += payload;
    }

    byte[] toSignByte = Encoding.UTF8.GetBytes(toSign);
        string signature = "";
    using (HMACSHA512 hmac = new HMACSHA512(base64Secret))
    {
        byte[] signatureBytes = hmac.ComputeHash(toSignByte);
        signature = Convert.ToBase64String(signatureBytes);
    }

        httpWebRequest.Accept = "application/json";                
        httpWebRequest.Headers.Add("Accept-Charset", "UTF-8");
        httpWebRequest.ContentType = "application/json";                
        httpWebRequest.Headers.Add("apikey", "<my api key>");
        httpWebRequest.Headers.Add("timestamp", timestamp );
        httpWebRequest.Headers.Add("signature", signature);

        if (!string.IsNullOrEmpty(payload))
        {
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(payload);
                streamWriter.Flush();
            }
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        string str = JsonConvert.SerializeObject(httpResponse);
        string res = string.Empty;
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            res = streamReader.ReadToEnd();

        }
        Console.WriteLine(res.ToString());

Regards, Murali

muralispm commented 6 years ago

I got this resolved..

Regards, Murali.