Kraken API with VB Net "invalid key"



我正在尝试在这里转换C#代码(特别是"QueryPrivate"函数(: https://bitbucket.org/arrivets/krakenapi/src/cff138b017c38efde2db1a080fb765790a6d04c8/KrakenClient/KrakenClient.cs?at=master&fileviewer=file-view-default

到工作VB网络代码。我不断收到"无效密钥"响应。这是我想出的代码:

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

我无法弄清楚为什么这一直返回无效键,除了我编码键的方式必须与 C# 中的方式不同

代码中有几个问题,即将字符串和字符组合在一起。

要在 VB 中连接字符串,请使用&而不是"+">

您还使用了几个帮助函数进行哈希处理,这也可能存在问题。

最好的办法是同时在 C# 和 VB 中启动程序,并逐步执行每一行代码,直到找出两者之间的差异。

最新更新