【发布时间】:2019-10-17 16:48:22
【问题描述】:
当我尝试通过 VBA 代码在 excel 中检索一些数据时遇到问题。 我使用以下内容作为基础:https://github.com/BitMEX/api-connectors/tree/master/official-http/vba 它有效,我可以根据需要更新它并下订单(测试网)
我现在尝试检索这本书,但我总是得到“签名无效”作为响应。 你能帮助理解我做错了什么吗?
我想接收的数据如下: https://testnet.bitmex.com/api/explorer/#!/OrderBook/OrderBook_getL2
作为哈希函数,我使用上面提供的链接中提供的 HexHash 函数(它适用于“发布”指令,但不能使其适用于“GET”指令。
提前致谢
下面是工作代码(POST 函数):
Sub placeorder()
Dim Json, httpObject As Object
Dim nonce As Double
Dim verb, apiKey, apiSecret, signature, symbol, price, qty, url, postdata, replytext, nonceStr As String
' Set monotonically (w time) increasing nonce
nonce = DateDiff("s", "1/1/1970", Now)
' Set api key and secret
apiKey = "aaa"
apiSecret = "bbb"
' Build query
symbol = "XBTUSD"
price = 8000
qty = 1
verb = "POST"
url = "/api/v1/order"
postdata = "symbol=" & symbol & "&price=" & price & "&quantity=" & qty
' Stringize nonce
nonceStr = nonce
' Compute signature using hexhash script
signature = HexHash.HexHash(verb + url + nonceStr + postdata, apiSecret, "SHA256")
' Set up HTTP req with headers
Set httpObject = CreateObject("MSXML2.XMLHTTP")
httpObject.Open "POST", "https://testnet.bitmex.com" & url, False
httpObject.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
httpObject.setRequestHeader "api-nonce", nonceStr
httpObject.setRequestHeader "api-key", apiKey
httpObject.setRequestHeader "api-signature", signature
httpObject.Send (postdata)
' Catch response
replytext = httpObject.ResponseText
end sub()
下面是一个非工作代码(GET 函数):
Sub getorderbook2()
Dim Json, httpObject As Object
Dim nonce As Double
Dim verb, apiKey, apiSecret, signature, symbol, url, getdata, replytext,
depth As String
Dim nonceStr As String
' Set monotonically (w time) increasing nonce
nonce = DateDiff("s", "1/1/1970", Now)
' Set api key and secret
apiKey = "aaa"
apiSecret = "bbb"
' Build query
symbol = "XBTUSD"
depth = 3
verb = "GET"
url = "/api/v1/orderBook/L2"
getdata = "symbol=" & symbol & "&depth=" & depth
' Stringize nonce
nonceStr = nonce
' Compute signature using hexhash script
signature = HexHash.HexHash(verb + url + nonceStr + getdata, apiSecret, "SHA256")
' Set up HTTP req with headers
Set httpObject = CreateObject("MSXML2.XMLHTTP")
httpObject.Open "GET", "https://testnet.bitmex.com" & url, False
httpObject.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
httpObject.setRequestHeader "api-nonce", nonceStr
httpObject.setRequestHeader "api-key", apiKey
httpObject.setRequestHeader "api-signature", signature
httpObject.Send (getdata)
' Catch response
replytext = httpObject.ResponseText
end sub ()
在第二部分,我总是收到一条错误消息,返回“签名无效”
【问题讨论】:
-
老实说,我不太确定代码......如果没有所有事实,很难测试。但是,将这些代码并排放置,
"application/application/x-www-form-urlencoded"在第二个中似乎不正确......应该是"application/x-www-form-urlencoded"? -
谢谢,在我这边创建消息时,这是一个复制过去的问题。原始代码是正确的:“application/x-www-form-urlencoded”并且它仍然具有“签名无效”。当你说“没有所有事实”时,你能告诉我你需要什么额外的信息吗?如果需要,我可以直接加载 excel 文件:)
-
事实上,我指的是出于安全原因您不能发布的所有其他详细信息,即:密钥、秘密等。我并不是说您还没有尽可能多地添加。另外,调试 API 并不是我的强项,只是想对我能够轻松测试的位发表评论。
-
@Bounty25 所以如果我理解正确,代码本身没有问题。它运行得很好,但是如果你打印
replytext,你会得到“签名无效”而不是你通常期望得到的 HTML 响应?如果是这种情况,您计算signature的方式/逻辑可能存在问题。 -
是的,你是对的,我得到«签名无效»。这让我很抓狂,因为我在第一个代码中使用相同的方法计算签名并且它有效,所以我不明白我在第二个代码中做错了什么。