Bitcoin Forum
May 22, 2024, 05:59:36 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: C#AND POLONIEX API CONNECT  (Read 3136 times)
AMONRA75 (OP)
Full Member
***
Offline Offline

Activity: 148
Merit: 100


View Profile
August 19, 2016, 04:00:03 PM
Last edit: August 19, 2016, 05:02:36 PM by AMONRA75
 #1

hi,

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




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!
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
August 19, 2016, 05:44:24 PM
 #2

this header is extra
Code:
webRequest.Headers.Add("command", "returnDepositAddresses");

i have WebRequest class Cheesy

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)

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
AMONRA75 (OP)
Full Member
***
Offline Offline

Activity: 148
Merit: 100


View Profile
August 19, 2016, 10:39:06 PM
 #3

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
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
August 21, 2016, 03:34:38 PM
 #4

as i said i hate WebRequest it is too long Cheesy

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

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
starmman
Legendary
*
Offline Offline

Activity: 1484
Merit: 1029



View Profile
February 17, 2017, 08:53:05 PM
 #5

as i said i hate WebRequest it is too long Cheesy

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.
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
February 18, 2017, 05:18:58 AM
 #6

Glad I could help ^^

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
Luser
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
June 04, 2017, 05:05:47 PM
 #7

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!
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
June 06, 2017, 03:40:31 AM
 #8

I am glad that this code is still helping people Smiley

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
matscol
Newbie
*
Offline Offline

Activity: 49
Merit: 0


View Profile
July 21, 2017, 11:26:54 PM
 #9

I'll add my thanks. This helped set up CB/GDAX api access as well.
SomeOneMore
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
January 05, 2018, 05:41:50 PM
 #10

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
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
January 06, 2018, 05:59:21 PM
 #11

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.

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
SomeOneMore
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
January 06, 2018, 09:30:13 PM
 #12

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
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!