Hi, I´m experimenting with API calls from an crypto exchange, here are the related c# examples:
I got the PUBLIC API part working. I can do the stuff with the request in the PRIVATE API part, but I have problems creating the authorization header correctly because of some features which won´t work inside UNITY, also I´m very confused how to convert this part.
the code example from the site:
string apiKey = "my key";
string apiSecret = "my secret";
string requestUri = "https://www.cryptopia.co.nz/Api/GetBalance";
var postData = new
{
Currency = "DOT"
};
// Create Request
var request = new HttpRequestMessage();
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(requestUri);
request.Content = new ObjectContent(typeof(object), postData, new JsonMediaTypeFormatter());
// Authentication
string requestContentBase64String = string.Empty;
if (request.Content != null)
{
// Hash content to ensure message integrity
using (var md5 = MD5.Create())
{
requestContentBase64String = Convert.ToBase64String(md5.ComputeHash(await request.Content.ReadAsByteArrayAsync()));
}
}
//create random nonce for each request
var nonce = Guid.NewGuid().ToString("N");
//Creating the raw signature string
var signature = Encoding.UTF8.GetBytes(string.Concat(apiKey, HttpMethod.Post, HttpUtility.UrlEncode(request.RequestUri.AbsoluteUri.ToLower()), nonce, requestContentBase64String));
using (var hmac = new HMACSHA256(Convert.FromBase64String(apiSecret)))
{
request.Headers.Authorization = new AuthenticationHeaderValue("amx", string.Format("{0}:{1}:{2}", apiKey, Convert.ToBase64String(hmac.ComputeHash(signature)), nonce));
}
// Send Request
using (var client = new HttpClient())
{
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
//{"Success":true,"Error":null,"Data":[{"CurrencyId":2,"Symbol":"DOT","Total":9646.07411016,"Available":9646.07411016,"Unconfirmed":0.0,"HeldForTrades":0.0,"PendingWithdraw":0.0,"Address":"1HEfio1kreDBgj5uCw4VHbEDSgc6YJXfTN","Status":"OK","StatusMessage":null}]}
}
}
my code looks like this:
void Start()
{
var postData = new
{
Currency = "DOT"
};
StartCoroutine(postRequest("http://www.cryptopia.co.nz/api/GetBalance", postData.ToString()));
}
IEnumerator postRequest(string url, string json)
{
var uwr = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
string requestContentBase64String = string.Empty;
var md5 = MD5.Create();
requestContentBase64String = Convert.ToBase64String(md5.ComputeHash(bodyRaw));
var nonce = Guid.NewGuid().ToString("N");
var signature = Encoding.UTF8.GetBytes(string.Concat(apiKey, UnityWebRequest.Post, UnityWebRequest.UrlEncode(url.ToLower()), nonce, requestContentBase64String));
using (var hmac = new HMACSHA256(Convert.FromBase64String(apiSecret)))
{
uwr.SetRequestHeader("amx", string.Format("{0}:{1}:{2}", apiKey, Convert.ToBase64String(hmac.ComputeHash(signature)), nonce));
}
uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
//Send the request then wait here until it returns
yield return uwr.Send();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
Debug.Log("Received: " + uwr.downloadHandler.text);
}
}
I´m really sure that this is tollay wrong because it gives me a “succesful= false error”
I think i have to use the WWW method to define the correct authorization header of Unity, but how?
APIKEY and APISECRET are the API key and PW creatd by the plattform…