Author

Topic: Bter API in C# - Bounty Offered (Read 2366 times)

hero member
Activity: 546
Merit: 500
October 28, 2014, 03:56:19 AM
#14
I also made same API call in C# for bittrex if someone needs.

Code:
        string bittrex_query(string path, NameValueCollection req, string key, string secret)
        {
            using (var client = new WebClient())
            {
                // nonce not really needed right now, can be also empty string
                string nonce= ((int)(DateTime.Now.Subtract(new DateTime(1970,1,1,0,0,0, DateTimeKind.Utc))).TotalSeconds).ToString();

                //add nonce and apiKey to req
                req["apikey"] = key;
                req["nonce"] = nonce;

                // Convert from NameValueCollection to string ready for post.
                var parameters = new StringBuilder();
                foreach (string ReqKey in req.Keys)
                    parameters.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(ReqKey), HttpUtility.UrlEncode(req[ReqKey]));
                if (parameters.Length > 0)
                    parameters.Length -= 1;
                string reqToString = parameters.ToString();

                //make proper uri
                string uri = "https://bittrex.com/api/v1.1/" + path + "?" + reqToString;

                var headers = new NameValueCollection();

                var keyByte = Encoding.UTF8.GetBytes(secret);
                using (HMACSHA512 hmac = new HMACSHA512(keyByte))
                {
                    var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(uri));
                    StringBuilder sBuilder = new StringBuilder();

                    for (int i = 0; i < hash.Length; i++)
                        sBuilder.Append(hash[i].ToString("x2"));

                    headers["apisign"] = sBuilder.ToString();
                }

                client.Headers.Add(headers);

                return client.DownloadString(uri);
            }
        }

And call it like that:
Code:
MessageBox.Show(bittrex_query(@"account/getbalances", new NameValueCollection(), textBoxAPIKEY.Text, textBoxSECRET.Text));
MessageBox.Show(bittrex_query(@"account/getbalance", new NameValueCollection { { "currency", "BTC" } }, textBoxAPIKEY.Text, textBoxSECRET.Text));
MessageBox.Show(bittrex_query(@"account/getdepositaddress", new NameValueCollection { { "currency", "BTC" } }, textBoxAPIKEY.Text, textBoxSECRET.Text));
hero member
Activity: 546
Merit: 500
October 07, 2014, 12:53:43 PM
#13
I appreciate all amounts, so thank you Smiley
It was also good exercise for me as I have never worked with APIs.
newbie
Activity: 80
Merit: 0
October 07, 2014, 12:36:36 PM
#12
Sent, sorry took longer than you expected. Not much, but it's what I can spare Wink
hero member
Activity: 546
Merit: 500
October 06, 2014, 01:13:56 PM
#11
Here is my address: 1PdHRDrRxBsohPRJMxTa5cVLNriEbJUhdP

Thank you  Smiley
newbie
Activity: 80
Merit: 0
October 06, 2014, 01:03:53 PM
#10
Just edited query function so it should now behave the same as their php function in api example. Again I can't test it due to no funds but I got message that order number is incorrect, so I assume that it is ok.

function:
Code:
       string bter_query(string path, NameValueCollection req, string key, string secret)
        {
            using (var client = new WebClient())
            {
                // Convert from NameValueCollection to string ready for post.
                var parameters = new StringBuilder();
                foreach (string ReqKey in req.Keys)
                    parameters.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(ReqKey), HttpUtility.UrlEncode(req[ReqKey]));
                if(parameters.Length>0)
                    parameters.Length -= 1;
                string reqToString = parameters.ToString();

                var headers = new NameValueCollection();
                headers["KEY"] = key;

                var keyByte = Encoding.UTF8.GetBytes(secret);
                using (HMACSHA512 hmac = new HMACSHA512(keyByte))
                {
                    var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(reqToString));
                    StringBuilder sBuilder = new StringBuilder();

                    for (int i = 0; i < hash.Length; i++)
                        sBuilder.Append(hash[i].ToString("x2"));

                    headers["SIGN"] = sBuilder.ToString();
                }

                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                client.Headers.Add(headers);
                var response = client.UploadValues("https://bter.com/api/" + path, req);

                return Encoding.Default.GetString(response);
            }

Run it like that:
Code:
MessageBox.Show(bter_query(@"1/private/orderlist", new NameValueCollection(), textBoxApiKey.Text, textBoxApiSecret.Text));
MessageBox.Show(bter_query(@"1/private/cancelorder", new NameValueCollection{{"order_id","125811"}}, textBoxApiKey.Text, textBoxApiSecret.Text));

Thanks it works. Ill review the code to see what I was doing wrong. Please post your BTC Addr.
hero member
Activity: 546
Merit: 500
October 05, 2014, 06:23:11 PM
#9
Just edited query function so it should now behave the same as their php function in api example. Again I can't test it due to no funds but I got message that order number is incorrect, so I assume that it is ok.

function:
Code:
       string bter_query(string path, NameValueCollection req, string key, string secret)
        {
            using (var client = new WebClient())
            {
                // Convert from NameValueCollection to string ready for post.
                var parameters = new StringBuilder();
                foreach (string ReqKey in req.Keys)
                    parameters.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(ReqKey), HttpUtility.UrlEncode(req[ReqKey]));
                if(parameters.Length>0)
                    parameters.Length -= 1;
                string reqToString = parameters.ToString();

                var headers = new NameValueCollection();
                headers["KEY"] = key;

                var keyByte = Encoding.UTF8.GetBytes(secret);
                using (HMACSHA512 hmac = new HMACSHA512(keyByte))
                {
                    var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(reqToString));
                    StringBuilder sBuilder = new StringBuilder();

                    for (int i = 0; i < hash.Length; i++)
                        sBuilder.Append(hash[i].ToString("x2"));

                    headers["SIGN"] = sBuilder.ToString();
                }

                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                client.Headers.Add(headers);
                var response = client.UploadValues("https://bter.com/api/" + path, req);

                return Encoding.Default.GetString(response);
            }

Run it like that:
Code:
MessageBox.Show(bter_query(@"1/private/orderlist", new NameValueCollection(), textBoxApiKey.Text, textBoxApiSecret.Text));
MessageBox.Show(bter_query(@"1/private/cancelorder", new NameValueCollection{{"order_id","125811"}}, textBoxApiKey.Text, textBoxApiSecret.Text));
hero member
Activity: 546
Merit: 500
October 05, 2014, 05:58:48 PM
#8
Ok I tried something, but don't have any funds on bter so I can't really test, but when I run it I get message Success.

Here is function for bter query
Code:
string bter_query(string path, NameValueCollection req, string key, string secret)
        {
            using (var client = new WebClient())
            {
                // For other queries (getorder, changeorder etc) you need to change parameter
                // req to string (using HttpUtility.UrlEncode) so you can use ComputeHash,
                // for orderlist you don't need since req is empty.
                string reqToString = "";
                var headers = new NameValueCollection();
                headers["KEY"] = key;

                var keyByte = Encoding.UTF8.GetBytes(secret);
                using (HMACSHA512 hmac = new HMACSHA512(keyByte))
                {
                    var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(reqToString));
                    StringBuilder sBuilder = new StringBuilder();

                    for (int i = 0; i < hash.Length; i++)
                        sBuilder.Append(hash[i].ToString("x2"));

                    headers["SIGN"] = sBuilder.ToString();
                }

                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                client.Headers.Add(headers);
                var response = client.UploadValues("https://bter.com/api/" + path, req);

                return Encoding.Default.GetString(response);
            }
        }

And you can call it like that:
Code:
MessageBox.Show(bter_query(@"1/private/orderlist", new NameValueCollection(), textBoxApiKey.Text, textBoxApiSecret.Text));

Result:
hero member
Activity: 546
Merit: 500
October 05, 2014, 01:02:48 PM
#7
I was referring to this image


Anyway if I have time I will try to check code and tell you if I would have more luck than you.
newbie
Activity: 80
Merit: 0
October 05, 2014, 12:26:51 PM
#6
i'v seen example they were feeding nonce as parameters, and i was testing various ways trying to get it to work. the nonce doesn't matter, It still doesn't work.
hero member
Activity: 546
Merit: 500
October 05, 2014, 04:43:15 AM
#5
Code:
Url = "https://data.bter.com/api/1/private/orderlist?nonce=" + epochtime;

In their api is written that there is no parameters for "orderlist", so don't know why are you using
Code:
?nonce=" + epochtime
newbie
Activity: 80
Merit: 0
October 04, 2014, 06:44:34 PM
#4
bump?
newbie
Activity: 80
Merit: 0
October 04, 2014, 09:08:37 AM
#3
Why don't you send a message to a staff of this website,just like this https://bitcointalksearch.org/user/bterjiuer-336981
She is a customer service girl. I believe she will transfer your problem to technicman.

I actually submitted a ticket, and they told me to refer to the PHP example on the bottom of the page. Which did absolutely nothing to help me Sad
hero member
Activity: 574
Merit: 500
October 04, 2014, 03:05:10 AM
#2
Why don't you send a message to a staff of this website,just like this https://bitcointalksearch.org/user/bterjiuer-336981
She is a customer service girl. I believe she will transfer your problem to technicman.
newbie
Activity: 80
Merit: 0
October 03, 2014, 10:56:01 PM
#1
I've been stuck on this forever now. I can't for the life figure out what I am doing wrong. I'm just trying to make a simple trade api query using BTER api. Here is my code. Bare with me, I'm fairly new to C#.
Ill give whoever shows me the way a small reward Wink
If im doing something wrong or improper let me know please, so I can learn and fix it :x

string bterkey = "xxx";
string btersecret= "xxx";
string Url;        
WebClient request = new WebClient();
DateTime dtCurTime = DateTime.Now;
DateTime dtEpochStartTime = Convert.ToDateTime("1/1/1970 8:00:00 AM");
TimeSpan ts = dtCurTime.Subtract(dtEpochStartTime);
long epochtime;
epochtime = ((((((ts.Days * 24) + ts.Hours) * 60) + ts.Minutes) * 60) + ts.Seconds);
Url = "https://data.bter.com/api/1/private/orderlist?nonce=" + epochtime;
var dataStr = Url;
request.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
request.Headers.Add("key", bterkey);
var hmac = new HMACSHA512(Encoding.ASCII.GetBytes(btersecret));
var messagebyte = Encoding.ASCII.GetBytes(Url);
var hashmessage = hmac.ComputeHash(messagebyte);
var sign = BitConverter.ToString(hashmessage).Replace("-", "");
request.Headers.Add("sign", sign);
byte[] bret = request.UploadData(Url, "post", System.Text.Encoding.ASCII.GetBytes(""));
string sret = System.Text.Encoding.ASCII.GetString(bret);
Jump to: