【问题标题】:"Signature not valid" when getting API data (VBA-Excel)获取 API 数据时“签名无效”(VBA-Excel)
【发布时间】: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 的方式/逻辑可​​能存在问题。
  • 是的,你是对的,我得到«签名无效»。这让我很抓狂,因为我在第一个代码中使用相同的方法计算签名并且它有效,所以我不明白我在第二个代码中做错了什么。

标签: excel vba api sha256 sha


【解决方案1】:

在 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。我建议的更改应该会导致生成正确的签名。如果您需要在 VBA 中对数据进行 URL 编码,那么this answer 可能会有所帮助。

其他问题:

  • Dim a, b As String 等价于 Dim a As Variant, b As String。要声明多个 String 变量,您需要编写 Dim a As String, b As String
  • CreateObject("MSXML2.XMLHTTP") 访问旧版本的 MSXML2 3.0。要访问最新的 6.0 版本,您需要 CreateObject("MSXML2.XMLHTTP.6.0")

【讨论】:

  • 非常感谢您的回答。我今天无法实现它,但明天会尝试并让您知道。
猜你喜欢
  • 2019-11-13
  • 2014-12-03
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 2018-01-12
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多