-
Jul 12th, 2017, 08:22 PM
#1
Thread Starter
Lively Member
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!
-
Jan 31st, 2018, 03:46 AM
#2
New Member
Re: Help with Kraken API POST Method
-
Jan 31st, 2018, 10:37 AM
#3
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.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Jan 31st, 2018, 02:22 PM
#4
New Member
Re: Help with Kraken API POST Method
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.
I'm not sure why OP put it there.
Me neither, after test, the C# function works well without this character
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|