"Signature not valid" 获取 API 数据时 (VBA-Excel)
"Signature not valid" when getting API data (VBA-Excel)
当我尝试通过 VBA 代码检索 excel 中的某些数据时遇到问题。
我使用以下作为基础:https://github.com/BitMEX/api-connectors/tree/master/official-http/vba
它有效,我能够根据需要更新它并下订单(testnet)
我现在尝试取回这本书,但我总是得到 "Signature not valid" 作为回应。
你能帮忙理解我做错了什么吗?
我要接收的数据如下:
https://testnet.bitmex.com/api/explorer/#!/OrderBook/OrderBook_getL2
作为哈希函数,我使用了上面提供的 link 中可用的 HexHash 函数(它适用于 'Post' 指令,但不能使其适用于 "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 ()
在第二部分,我总是在 return "Signature not valid"
中收到错误消息
在 GET 和 POST 之间切换需要的不仅仅是更改请求中的动词。 GET 请求需要将数据作为 URL 字符串的一部分,因此请尝试:
url = url & "?" & getdata
getdata = ""
httpObject.Open "GET", "https://testnet.bitmex.com" & url, False
您还需要更改此行:
httpObject.Send (getdata)
至:
httpObject.Send
构造 api-signature
值的方式对于对此 API 的 GET 请求也是不同的 - 请参阅 here for details. The changes I have suggested should lead to the correct signature being generated. If you need to URL encode data in VBA then this answer 可能有所帮助。
其他问题:
Dim a, b As String
等同于 Dim a As Variant, b As String
。要声明多个字符串变量,您需要编写 Dim a As String, b As String
CreateObject("MSXML2.XMLHTTP")
访问 MSXML2 的旧版本 3.0。要访问最新版本 6.0,您需要 CreateObject("MSXML2.XMLHTTP.6.0")
当我尝试通过 VBA 代码检索 excel 中的某些数据时遇到问题。 我使用以下作为基础:https://github.com/BitMEX/api-connectors/tree/master/official-http/vba 它有效,我能够根据需要更新它并下订单(testnet)
我现在尝试取回这本书,但我总是得到 "Signature not valid" 作为回应。 你能帮忙理解我做错了什么吗?
我要接收的数据如下: https://testnet.bitmex.com/api/explorer/#!/OrderBook/OrderBook_getL2
作为哈希函数,我使用了上面提供的 link 中可用的 HexHash 函数(它适用于 'Post' 指令,但不能使其适用于 "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 ()
在第二部分,我总是在 return "Signature not valid"
中收到错误消息在 GET 和 POST 之间切换需要的不仅仅是更改请求中的动词。 GET 请求需要将数据作为 URL 字符串的一部分,因此请尝试:
url = url & "?" & getdata
getdata = ""
httpObject.Open "GET", "https://testnet.bitmex.com" & url, False
您还需要更改此行:
httpObject.Send (getdata)
至:
httpObject.Send
构造 api-signature
值的方式对于对此 API 的 GET 请求也是不同的 - 请参阅 here for details. The changes I have suggested should lead to the correct signature being generated. If you need to URL encode data in VBA then this answer 可能有所帮助。
其他问题:
Dim a, b As String
等同于Dim a As Variant, b As String
。要声明多个字符串变量,您需要编写Dim a As String, b As String
CreateObject("MSXML2.XMLHTTP")
访问 MSXML2 的旧版本 3.0。要访问最新版本 6.0,您需要CreateObject("MSXML2.XMLHTTP.6.0")