Bitcoin Forum

Economy => Exchanges => Topic started by: AMONRA75 on August 19, 2016, 04:00:03 PM



Title: C#AND POLONIEX API CONNECT
Post by: AMONRA75 on August 19, 2016, 04:00:03 PM
hi,

i need an help to connect to poloniex api (non public) with c#
anyone can help me ???




Code:
 const string WEBSERVICE_URL = "https://poloniex.com/tradingApi";
            try
            {
                var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
                if (webRequest != null)
                {
                    webRequest.Method = "POST";
                    webRequest.Timeout = 12000;
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                    webRequest.Headers.Add("Key", _apiKey);
                    webRequest.Headers.Add("Sign", CreateSignature());
                    webRequest.Headers.Add("command", "returnDepositAddresses");
                    

                    using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                        {
                            var jsonResponse = sr.ReadToEnd();
                            Console.WriteLine(String.Format("Response: {0}", jsonResponse));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }



i see the error

Code:

"Response: {"error":"Invalid command."}"

tnx a lot!


Title: Re: C#AND POLONIEX API CONNECT
Post by: Coding Enthusiast on August 19, 2016, 05:44:24 PM
this header is extra
Code:
webRequest.Headers.Add("command", "returnDepositAddresses");

i have WebRequest class :D

you should put the "command" and "returnDepositAddresses" in httpcontent and sign that, then add the signature to the header "sign"
i have no idea what you are doing here:
Code:
webRequest.Headers.Add("Sign", CreateSignature());
what does that function sign without having any input variable?


PHP is the closest example you can see and kind of understand when using c# IMO: http://pastebin.com/iuezwGRZ (see line 28 and 29)


Title: Re: C#AND POLONIEX API CONNECT
Post by: AMONRA75 on August 19, 2016, 10:39:06 PM
i write the complete code

Code:
        
        private readonly string _apiKey = "N68R67PJ-BZ89CNWB-O2UOOH59-YYFSX9MB"; // Random numbers and letters
        private readonly string _apiSecret = "345eaefc0bf5fbb5a048de7f8dc728646b61249f9c8fd9c1a99319e3ed0d55a6b8e43702cea379b2d136fe9b877f132706ceebc2930b220322b23a207151ea78";
        private long nonce = DateTime.Now.Ticks;




        private string CreateSignature()
        {
            //string msg = string.Format("{0}{1}{2}", _apiKey);

            return ByteArrayToString(SignHMACSHA512(_apiSecret, StringToByteArray(_apiKey))).ToUpper();
        }

        private static byte[] SignHMACSHA512(String key, byte[] data)
        {
            HMACSHA512 hashMaker = new HMACSHA512(Encoding.ASCII.GetBytes(key));
            return hashMaker.ComputeHash(data);
        }

        private static byte[] StringToByteArray(string str)
        {
            return System.Text.Encoding.ASCII.GetBytes(str);
        }

        private static string ByteArrayToString(byte[] hash)  //rimuove - e converte in bite
        {
            return BitConverter.ToString(hash).Replace("-", "").ToLower();
        }





        private void button5_Click(object sender, EventArgs e)
        {

            const string WEBSERVICE_URL = "https://poloniex.com/tradingApi";
            try
            {


                var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
                if (webRequest != null)
                {
                    webRequest.Method = "POST";
                    webRequest.Timeout = 12000;
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                    webRequest.Headers.Add("Key", _apiKey);
                    webRequest.Headers.Add("Sign", CreateSignature());     // keysecret
                   
                    var postData = "&nonce=&command=returnBalances";
                    var data = Encoding.ASCII.GetBytes(postData);
                   



                    using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                        {
                            var jsonResponse = sr.ReadToEnd();
                            Console.WriteLine(String.Format("Response: {0}", jsonResponse));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }



i have remove the extra headers but see the error


Title: Re: C#AND POLONIEX API CONNECT
Post by: Coding Enthusiast on August 21, 2016, 03:34:38 PM
as i said i hate WebRequest it is too long :D

but here i made you a complete code for it using HttpClient only in 30 lines
https://gist.github.com/Coding-Enthusiast/863f46ceef57ff2e1cf18adb8b34d926


Title: Re: C#AND POLONIEX API CONNECT
Post by: starmman on February 17, 2017, 08:53:05 PM
as i said i hate WebRequest it is too long :D

but here i made you a complete code for it using HttpClient only in 30 lines
https://gist.github.com/Coding-Enthusiast/863f46ceef57ff2e1cf18adb8b34d926

Thanks for sharing that link - you saved me a lot of time getting the signature generated, first API I tried since Cryptsy - seems line every API has its quirks.


Title: Re: C#AND POLONIEX API CONNECT
Post by: Coding Enthusiast on February 18, 2017, 05:18:58 AM
Glad I could help ^^


Title: Re: C#AND POLONIEX API CONNECT
Post by: Luser on June 04, 2017, 05:05:47 PM
Coding Enthusiast,
I registered on this site only so I could say thank you for posting this!
Dang, I would have been stuck for days on this, so thanks a big bunch to you!

Now I can proceed in automatically losing my altcoins instead of having to throw them away manually! =)

Best regards
Luser

PS. Thanks to Amonra75 too for asking, of course. The web is truly awesome at times!


Title: Re: C#AND POLONIEX API CONNECT
Post by: Coding Enthusiast on June 06, 2017, 03:40:31 AM
I am glad that this code is still helping people :)


Title: Re: C#AND POLONIEX API CONNECT
Post by: matscol on July 21, 2017, 11:26:54 PM
I'll add my thanks. This helped set up CB/GDAX api access as well.


Title: Re: C#AND POLONIEX API CONNECT
Post by: SomeOneMore on January 05, 2018, 05:41:50 PM
can you please put the code for call the command "returnOpenOrders"?

"Returns your open orders for a given market, specified by the "currencyPair" POST parameter, e.g. "BTC_XCP". Set "currencyPair" to "all" to return open orders for all markets. Sample output for single market:"

Thanks in advance


Title: Re: C#AND POLONIEX API CONNECT
Post by: Coding Enthusiast on January 06, 2018, 05:59:21 PM
Just change line #6 command from "returnBalances" to "returnOpenOrders" and add an optional parameter after it and before nonce, "&currencyPair=BTC_XCP".
It should work. I don't have a poloniex account anymore to test it.


Title: Re: C#AND POLONIEX API CONNECT
Post by: SomeOneMore on January 06, 2018, 09:30:13 PM
Just change line #6 command from "returnBalances" to "returnOpenOrders" and add an optional parameter after it and before nonce, "&currencyPair=BTC_XCP".
It should work. I don't have a poloniex account anymore to test it.

it works. thanks