[enter image description here][1]Help to correct it please C# KrakenApi writing like "Invalid Key" but key is valid. I just have no idea how to fix it, but I need it so much.
MAIN: Create a URL for the Kraken Balance API
var url = "https://api.kraken.com/0/private/Balance";
Create a request object
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
Add the API key header
string apiKey = "API KEY";
request.Headers.Add("API-Key", apiKey);
Add the secret key header
string secret = "API SIGN";
var nonce = GenerateNonce();
var postData = $"nonce={nonce}";
var apiSign = ComputeSignature(url, nonce, postData, secret);
request.Headers.Add("API-Sign", apiSign);
Write the POST data to the request body
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
Get the response from the API
try
{
using (var response = (HttpWebResponse)request.GetResponse())
{
// Read the response stream
using (var reader = new StreamReader(response.GetResponseStream()))
{
// Convert the response to a string
var responseString = reader.ReadToEnd();
// Print the response
Console.WriteLine(responseString);
}
}
}
catch (WebException ex)
{
If an error occurred, print the error message
using (var errorResponse = (HttpWebResponse)ex.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
var error = reader.ReadToEnd();
Console.WriteLine(error);
}
}
}
methods:
static string ComputeSignature(string url, string nonce, string postData, string secret)
{
var path = new Uri(url).AbsolutePath;
var data = Encoding.UTF8.GetBytes(nonce + postData);
using (var hmac = new HMACSHA512(Convert.FromBase64String(secret)))
{
var pathBytes = Encoding.UTF8.GetBytes(path);
hmac.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
hmac.TransformFinalBlock(data, 0, data.Length);
var hash = hmac.Hash;
var signature = Convert.ToBase64String(hash);
return signature;
}
}
static string GenerateNonce()
{
var nonce = DateTime.UtcNow.Ticks;
return nonce.ToString();
}
I tried get my balance in Kraken.com with API
tried some, but -, may be u can advice some from nuget packages
public static byte[] ComputeSha256Hash(string nonce, string inputParams)
{
string combinedString = nonce + inputParams;
byte[] data = Encoding.UTF8.GetBytes(combinedString);
using (SHA256 sha256Hash = SHA256.Create())
{
byte[] hashBytes = sha256Hash.ComputeHash(data);
return hashBytes;
}
}
public static byte[] ComputeSha512Hash(byte[] sha256Hash, string endpointName)
{
byte[] endpointBytes = Encoding.UTF8.GetBytes(endpointName);
byte[] combinedBytes = new byte[sha256Hash.Length + endpointBytes.Length];
Buffer.BlockCopy(sha256Hash, 0, combinedBytes, 0, sha256Hash.Length);
Buffer.BlockCopy(endpointBytes, 0, combinedBytes, sha256Hash.Length, endpointBytes.Length);
using (SHA512 sha512Hash = SHA512.Create())
{
byte[] hashBytes = sha512Hash.ComputeHash(combinedBytes);
return hashBytes;
}
}