I'm having trouble getting a POST code to work for the Kraken API. My issue is in the way the API-SIGN is encrypted/sent in the HTTP Header I think. Here is what the HTTP header is SUPPOSED to be sent as:
API-Key = API key
API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
Also shown here https://www.kraken.com/help/api#general-usage
And here are the three functions I've put together by working through other examples and POST programs I've built in the past:
Running the QueryPrivate function for say the account balances, gives me back an invalid key response. I'm not great with these encryptions. Hopefully someone else is better at them then me!Code:Private Function QueryPrivate(a_sMethod As String, Optional props As String = Nothing) As String ' generate a 64 bit nonce using a timestamp at tick resolution _url = "https://api.kraken.com" _version = 0 _key = "Key Here" _secret = "Secret Here" _rateGate = New RateGate(1, TimeSpan.FromSeconds(5)) Dim nonce As Int64 = DateTime.Now.Ticks props = Convert.ToString("nonce=" + nonce.ToString()) & props Dim path As String = String.Format("/{0}/private/{1}", _version, a_sMethod) Dim address As String = _url & path Dim webRequest__1 As HttpWebRequest = DirectCast(WebRequest.Create(address), HttpWebRequest) webRequest__1.ContentType = "application/x-www-form-urlencoded" webRequest__1.Method = "POST" webRequest__1.Headers.Add("API-Key", _key) Dim base64DecodedSecred As Byte() = Convert.FromBase64String(_secret) Dim np = nonce.ToString() + Convert.ToChar(0) + props Dim pathBytes = Encoding.UTF8.GetBytes(path) Dim hash256Bytes = sha256_hash(np) Dim z = New Byte(pathBytes.Count() + (hash256Bytes.Count() - 1)) {} pathBytes.CopyTo(z, 0) hash256Bytes.CopyTo(z, pathBytes.Count()) Dim signature = getHash(base64DecodedSecred, z) webRequest__1.Headers.Add("API-Sign", Convert.ToBase64String(signature)) If props IsNot Nothing Then Using writer = New StreamWriter(webRequest__1.GetRequestStream()) writer.Write(props) End Using End If 'Make the request Try 'Wait for RateGate _rateGate.WaitToProceed() Using webResponse As WebResponse = webRequest__1.GetResponse() Using str As Stream = webResponse.GetResponseStream() Using sr As New StreamReader(str) Dim responseContent3 As String = sr.ReadToEnd Return responseContent3 End Using End Using End Using Catch wex As WebException Using response As HttpWebResponse = DirectCast(wex.Response, HttpWebResponse) Using str As Stream = response.GetResponseStream() Using sr As New StreamReader(str) Dim responseContent3 As String = sr.ReadToEnd Return responseContent3 End Using End Using End Using End Try End Function Private Function sha256_hash(value As [String]) As Byte() Using hash As SHA256 = SHA256Managed.Create() Dim enc As Encoding = Encoding.UTF8 Dim result As [Byte]() = hash.ComputeHash(enc.GetBytes(value)) Return result End Using End Function Private Function getHash(keyByte As Byte(), messageBytes As Byte()) As Byte() Using hmacsha512 = New HMACSHA512(keyByte) Dim result As [Byte]() = hmacsha512.ComputeHash(messageBytes) Return result End Using End Function




Reply With Quote
