C# POST API call

irmscher

Client
Регистрация
28.11.2012
Сообщения
199
Благодарностей
6
Баллы
18
Hey guys,

Anyone can please help me to create this POST API Call using Zenno C# block?

All calls to the trading API are sent via HTTP POST to https://poloniex.com/tradingApi and must contain the following headers:

  • Key - Your API key.
  • Sign - The query's POST data signed by your key's "secret" according to the HMAC-SHA512 method.
Additionally, all queries must include a "nonce" POST parameter. The nonce parameter is an integer which must always be greater than the previous nonce used.

Thanks a lot!
 

irmscher

Client
Регистрация
28.11.2012
Сообщения
199
Благодарностей
6
Баллы
18
Ok, I got help from the internet. This code seems work as a normal C# code, however it gives a bunch of errors when I put it into a zennoposter C# block:

Код:
const string ApiKey = "YourAPIKey";
const string ApiSecret = "YourAPISecret";
public async void MyFunc()
        {
            string url = "https://poloniex.com/tradingApi";
            string myParam = "command=returnBalances&nonce=" + nonce();
            string result = await SendPrivateApiRequestAsync(url, myParam);
            //deserialize the result string to your liking
        }
private async Task<string> SendPrivateApiRequestAsync(string privUrl, string myParam)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(privUrl);
                StringContent myContent = new StringContent(myParam);
                myContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                client.DefaultRequestHeaders.Add("Key", ApiKey);
                client.DefaultRequestHeaders.Add("Sign", genHMAC(myParam));
                var result = await client.PostAsync(privUrl, myContent);
                return await result.Content.ReadAsStringAsync();
            }
        }
private string genHMAC(string message)
        {
            var hmac = new HMACSHA512(Encoding.ASCII.GetBytes(ApiSecret));
            var messagebyte = Encoding.ASCII.GetBytes(message);
            var hashmessage = hmac.ComputeHash(messagebyte);
            var sign = BitConverter.ToString(hashmessage).Replace("-", "").ToLower();
            return sign;
        }
 

irmscher

Client
Регистрация
28.11.2012
Сообщения
199
Благодарностей
6
Баллы
18

Radzhab

Client
Регистрация
23.05.2014
Сообщения
1 500
Благодарностей
1 268
Баллы
113
Write to skype ahvahsky2008. I help to you
 
  • Спасибо
Реакции: irmscher

irmscher

Client
Регистрация
28.11.2012
Сообщения
199
Благодарностей
6
Баллы
18
Sent you a contact request :-)
 

EtaLasquera

Client
Регистрация
02.01.2017
Сообщения
526
Благодарностей
112
Баллы
43
Too hardcore, try pass parameters in "Data" at actions proprieties of post block.
 

irmscher

Client
Регистрация
28.11.2012
Сообщения
199
Благодарностей
6
Баллы
18
Too hardcore, try pass parameters in "Data" at actions proprieties of post block.
Yes, I did. Seems done everything properly, but still getting blank API responses like this:

Код:
HTTP/1.1 404 Not Found
Date: Tue, 19 Dec 2017 14:42:40 GMT
Content-Length: 0
Connection: keep-alive
Set-Cookie: __cfduid=d8be3b48cbe35309ec417cbd8bd5412d11513694560; expires=Wed, 19-Dec-18 14:42:40 GMT; path=/; domain=.liqui.io; HttpOnly; Secure
Server: cloudflare-nginx
CF-RAY: 3cfb213b5dda86f1-ARN
Yes, no error (like {"error":"<error message>"}) but no other output either...

So I went back and read the API specifications carefully one more time:

All calls to the trading API are sent via HTTP POST to https://poloniex.com/tradingApi and must contain the following headers:
  • Key - Your API key.
  • Sign - The query's POST data signed by your key's "secret" according to the HMAC-SHA512 method.
Additionally, all queries must include a "nonce" POST parameter. The nonce parameter is an integer which must always be greater than the previous nonce used.

source: https://poloniex.com/support/api/
OK, triple-checked my template, and I really struggle to figure out what I did wrong. Here's my template:



Any ideas what I did wrong?

And yes, I tried to include my Key for HMAC-SHA512 encryption instead of putting it straight to HTTP POST block. And yes, tried both ASCII and UTF8 encoding in the HMAC-SHA512 process.


 

EtaLasquera

Client
Регистрация
02.01.2017
Сообщения
526
Благодарностей
112
Баллы
43
I can't help you since your private keys is a your secret :-)
But check your nonce var because Zenno Poster TimeNow automatic pad left zeros.
As example, my clock now:
2017/12/12 09:09am
121299 << your nonce

In next month...
2018/01/01 09:09am
1199 << your nonce

Consider use return DateTime.Now.ToString("yyyyMMddHHmmss");
 
Последнее редактирование:

irmscher

Client
Регистрация
28.11.2012
Сообщения
199
Благодарностей
6
Баллы
18
Connect to Bittrex API with C#

OK, I finally figured out myself how to do this:

Код:
string ApiKey = "yourAPIkey";
string ApiSecret = "yourSecret";
string nonce = DateTime.Now.ToString("HHmmss");
string url = "https://bittrex.com/api/v1.1/account/getbalance?apikey="+ApiKey+"&currency=BTC"+"&nonce="+nonce;

//byte[] secretkeyBytes = Encoding.UTF8.GetBytes(secretKey);
byte[] secretkeyBytes = Encoding.UTF8.GetBytes(ApiSecret);
byte[] inputBytes = Encoding.UTF8.GetBytes(url);
var hmac = new HMACSHA512(secretkeyBytes);
byte[] hashValue = hmac.ComputeHash(inputBytes); 
string signature = BitConverter.ToString(hashValue).Replace("-", "").ToLower();
//return signature;

var client = new WebClient();
client.Headers.Add("apisign", signature);
var content = client.DownloadString(url);
return content;
 
  • Спасибо
Реакции: lupo

iulius

Client
Регистрация
01.10.2011
Сообщения
57
Благодарностей
3
Баллы
8
Connect to Bittrex API with C#

OK, I finally figured out myself how to do this:

Код:
string ApiKey = "yourAPIkey";
string ApiSecret = "yourSecret";
string nonce = DateTime.Now.ToString("HHmmss");
string url = "https://bittrex.com/api/v1.1/account/getbalance?apikey="+ApiKey+"&currency=BTC"+"&nonce="+nonce;

//byte[] secretkeyBytes = Encoding.UTF8.GetBytes(secretKey);
byte[] secretkeyBytes = Encoding.UTF8.GetBytes(ApiSecret);
byte[] inputBytes = Encoding.UTF8.GetBytes(url);
var hmac = new HMACSHA512(secretkeyBytes);
byte[] hashValue = hmac.ComputeHash(inputBytes);
string signature = BitConverter.ToString(hashValue).Replace("-", "").ToLower();
//return signature;

var client = new WebClient();
client.Headers.Add("apisign", signature);
var content = client.DownloadString(url);
return content;
Did you try the same with Binance API? or other C# implementation of the API like this one https://github.com/morpheums/Binance.API.Csharp.Client
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)