Bitcoin Forum
October 18, 2017, 09:47:10 AM *
News: Latest stable version of Bitcoin Core: 0.15.0.1  [Torrent]. (New!)
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Bter API in C# - Bounty Offered  (Read 2199 times)
AstralAcc3ss
Jr. Member
*
Offline Offline

Activity: 42


View Profile
October 04, 2014, 03:56:01 AM
 #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);
1508320030
Hero Member
*
Offline Offline

Posts: 1508320030

View Profile Personal Message (Offline)

Ignore
1508320030
Reply with quote  #2

1508320030
Report to moderator
1508320030
Hero Member
*
Offline Offline

Posts: 1508320030

View Profile Personal Message (Offline)

Ignore
1508320030
Reply with quote  #2

1508320030
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
szmarco
Hero Member
*****
Offline Offline

Activity: 574



View Profile
October 04, 2014, 08:05:10 AM
 #2

Why don't you send a message to a staff of this website,just like this https://bitcointalk.org/index.php?action=profile;u=336981
She is a customer service girl. I believe she will transfer your problem to technicman.
AstralAcc3ss
Jr. Member
*
Offline Offline

Activity: 42


View Profile
October 04, 2014, 02:08:37 PM
 #3

Why don't you send a message to a staff of this website,just like this https://bitcointalk.org/index.php?action=profile;u=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
AstralAcc3ss
Jr. Member
*
Offline Offline

Activity: 42


View Profile
October 04, 2014, 11:44:34 PM
 #4

bump?
amacar
Hero Member
*****
Offline Offline

Activity: 546


View Profile
October 05, 2014, 09: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
AstralAcc3ss
Jr. Member
*
Offline Offline

Activity: 42


View Profile
October 05, 2014, 05: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.
amacar
Hero Member
*****
Offline Offline

Activity: 546


View Profile
October 05, 2014, 06: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.
amacar
Hero Member
*****
Offline Offline

Activity: 546


View Profile
October 05, 2014, 10: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:
amacar
Hero Member
*****
Offline Offline

Activity: 546


View Profile
October 05, 2014, 11: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));
AstralAcc3ss
Jr. Member
*
Offline Offline

Activity: 42


View Profile
October 06, 2014, 06: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.
amacar
Hero Member
*****
Offline Offline

Activity: 546


View Profile
October 06, 2014, 06:13:56 PM
 #11

Here is my address: 1PdHRDrRxBsohPRJMxTa5cVLNriEbJUhdP

Thank you  Smiley
AstralAcc3ss
Jr. Member
*
Offline Offline

Activity: 42


View Profile
October 07, 2014, 05:36:36 PM
 #12

Sent, sorry took longer than you expected. Not much, but it's what I can spare Wink
amacar
Hero Member
*****
Offline Offline

Activity: 546


View Profile
October 07, 2014, 05: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.
amacar
Hero Member
*****
Offline Offline

Activity: 546


View Profile
October 28, 2014, 08: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));
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!