Help with Kraken API POST Method
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:
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
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!
Re: Help with Kraken API POST Method
This message is from July, but since I have encountered exactly the same problem, I guess there are hundreds will run on the same brick wall. So I give what I found, to help the future reader of this message.
This sub has been translated from C # (the example given on the kraken site).
The C# compiled and referenced in my VB project works, the same sub translated in VB don't ! :duck::duck::duck:
After one day of mind-boggling, i logged each variable and i found this:
np = 636529969190892278nonce=636529969190892278 (C#)
np = 636529969355408169 nonce=636529969355408169 (VB)
Apparently, VB and C# don't interpret tochar(0) in the same way.
so i used:
Code:
Dim np = nonce & props
instead of
Code:
Dim np = nonce & Convert.ToChar(0) & props
and it's works :) ... enjoy
:bigyello:
I don't understand of course, anything in that, it would be nice that a specialist explain what happens with this ToChar(0). And why VB and C# don't understand this character in the same way.
Re: Help with Kraken API POST Method
I just tested and that is not what I observed, but perhaps it matters how you output the string.
The character with code '0' is generally "the null character", which shouldn't be printed at all. I don't see anything in that API documentation that says it should be there at all. I'm not sure why OP put it there.
Re: Help with Kraken API POST Method
Quote:
Originally Posted by
Sitten Spynne
I just tested and that is not what I observed, but perhaps it matters how you output the string.
i wrote them in a txt file.
Quote:
I'm not sure why OP put it there.
Me neither, after test, the C# function works well without this character