c # — интеграция PHP в .NET (winform) API вызывает некоторые проблемы

Я пытаюсь реализовать последнюю версию API Bittrex для небольшого инструмента, который я делаю для себя, у меня все в порядке, за исключением того, что они запрашивают это для V1.1:

    For this version, we use a standard HMAC-SHA512 signing. Append apikey and nonce to your request and calculate the HMAC hash and include it under an apisign header. Note: the nonce is not respected right now but will be enforced later.

$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);

Чтобы реализовать это, я сделал это:

// TODO : add a way to get this from the user
var apiKey = ApiKey;
var apiSecret = ApiSecret;

// var nonce = (int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; // same as time() in PHP
var encoding = Encoding.UTF8;
var url = @"https://bittrex.com/api/v1.1/account/getbalances?apikey=" + apiKey;
var result = Gethmacsha512(encoding, apiSecret, url);

// some var for the request
var account = new List<AccountCurrencies>();

// sending it to get the response
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("apisign:",result);
request.ContentType = "application/json";
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();

Resp.GetValue(stream, account);
return account;

Который возвращает меня, что HTTPHEADER не хорош.

Как видите, я использовал решения, которые нашел на этом сайте (Gethmacsha512, способ получить время от php до .Net, некоторые другие советы & трюки) но я просто не могу понять, каким образом я должен послать свой знак над ….

Если кто-то из вас может направить меня к решению, или указать мне, что я ищу (поскольку я не знаю, что такое curl), или даже набросать мне пример кода, который я мог бы изучить, чтобы понять эту часть, это было бы круто.

РЕДАКТИРОВАТЬ :

Я обновил выше, как следует:

// TODO : add a way to get this from the user
var apiKey = ApiKey;
var apiSecret = ApiSecret;

var nonce = (int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; // same as time() in PHP
var encoding = Encoding.UTF8;
var url = @"https://bittrex.com/api/v1.1/account/getbalances?apikey=" + apiKey;
var urlForAuth = url + "&nonce=" + nonce;
var result = Gethmacsha512(encoding, apiSecret, urlForAuth);

// some var for the request
var account = new List<AccountCurrencies>();

// sending it to get the response
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("apisign",result);
request.Headers.Add("nonce", nonce.ToString());
request.ContentType = "application/json";
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();

Resp.GetValue(stream, account);
return account;

Виновником проблемы выше было «apisign:», так как «:» был недопустимым символом, и я попытался интегрировать «nonce», потому что, пока все компилируется, аутентификация не удалась из-за того, что «nonce» не было отправлено. Поэтому я попытался добавить его в URL или в заголовок, но оба не удалось.

0

Решение

Таким образом, решение было на самом деле «довольно» простым. Если кто-то что-то делает с Bittrex API, вот код, который работает:

public static List<AccountCurrencies> GetAccountCurrencies()
{
if (Settings.Default.APIKey == null || Settings.Default.APISecret == null) return new List<AccountCurrencies>();
var nonce = (int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; // same as time() in PHP, need to integrate it
var encoding = Encoding.UTF8;
var urlForAuth = @"https://bittrex.com/api/v1.1/account/getbalances?apikey=" + Settings.Default.APIKey + "&nonce=" + nonce;
var result = Gethmacsha512(encoding, Settings.Default.APISecret, urlForAuth);

// some var for the request
var account = new List<AccountCurrencies>();

// sending it to get the response
var request = (HttpWebRequest)WebRequest.Create(urlForAuth);
request.Headers.Add("apisign",result);
//request.Headers.Add("nonce", nonce.ToString());
request.ContentType = "application/json";
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();

Resp.GetValue(stream, account);
return account;
}


private static string Gethmacsha512(Encoding encoding, string apiSecret, string url)
{
// doing the encoding
var keyByte = encoding.GetBytes(apiSecret);
string result;
using (var hmacsha512 = new HMACSHA512(keyByte))
{
hmacsha512.ComputeHash(encoding.GetBytes(url));

result = ByteToString(hmacsha512.Hash);
}
return result;
}

static string ByteToString(IEnumerable<byte> buff)
{
return buff.Aggregate("", (current, t) => current + t.ToString("X2"));
}
1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]