AstralAcc3ss (OP)
Newbie
Offline
Activity: 80
Merit: 0
|
|
October 04, 2014, 03:56:01 AM |
|
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 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);
|
|
|
|
|
|
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
|
|
AstralAcc3ss (OP)
Newbie
Offline
Activity: 80
Merit: 0
|
|
October 04, 2014, 02:08:37 PM |
|
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
|
|
|
|
AstralAcc3ss (OP)
Newbie
Offline
Activity: 80
Merit: 0
|
|
October 04, 2014, 11:44:34 PM |
|
bump?
|
|
|
|
amacar
|
|
October 05, 2014, 09:43:15 AM |
|
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
|
|
|
|
AstralAcc3ss (OP)
Newbie
Offline
Activity: 80
Merit: 0
|
|
October 05, 2014, 05:26:51 PM |
|
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.
|
|
|
|
amacar
|
|
October 05, 2014, 06:02:48 PM |
|
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.
|
|
|
|
amacar
|
|
October 05, 2014, 10:58:48 PM |
|
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 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: MessageBox.Show(bter_query(@"1/private/orderlist", new NameValueCollection(), textBoxApiKey.Text, textBoxApiSecret.Text));
Result:
|
|
|
|
amacar
|
|
October 05, 2014, 11:23:11 PM |
|
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: 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: 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));
|
|
|
|
AstralAcc3ss (OP)
Newbie
Offline
Activity: 80
Merit: 0
|
|
October 06, 2014, 06:03:53 PM |
|
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: 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: 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.
|
|
|
|
amacar
|
|
October 06, 2014, 06:13:56 PM |
|
Here is my address: 1PdHRDrRxBsohPRJMxTa5cVLNriEbJUhdP Thank you
|
|
|
|
AstralAcc3ss (OP)
Newbie
Offline
Activity: 80
Merit: 0
|
|
October 07, 2014, 05:36:36 PM |
|
Sent, sorry took longer than you expected. Not much, but it's what I can spare
|
|
|
|
amacar
|
|
October 07, 2014, 05:53:43 PM |
|
I appreciate all amounts, so thank you It was also good exercise for me as I have never worked with APIs.
|
|
|
|
amacar
|
|
October 28, 2014, 08:56:19 AM |
|
I also made same API call in C# for bittrex if someone needs. 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: 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));
|
|
|
|
|