使用VBA正确编码POST参数(WinHttp.WinHttpRequest.5.1)

时间:2017-12-27 15:08:35

标签: excel-vba api post vba excel

我试图通过Visual Basic访问Bitfinex API,但我没有实现对POST请求的编码。 我的代码是这样的:

Function PrivateBitfinex(Method As String, apikey As String, secretkey As     String, Optional MethodOptions As String) As String


Dim NonceUnique As String
Dim body As String
Dim payload As String
Dim signature As String

'Kraken nonce: 16 characters
NonceUnique = DateDiff("s", "1/1/1970", Now)
NonceUnique = NonceUnique & Right(Timer * 100, 2) & "0000"

TradeApiSite = "https://api.bitfinex.com"
urlPath = "/v1/" & Method
body = "{""request="": """ & urlPath & """, ""nonce="":" & NonceUnique & "}"
payload = Base64Encode(body)
signature = ComputeHash_C("SHA384", payload, secretkey, "STR64")

Url = TradeApiSite & urlPath

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "POST", Url, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "X-BFX-APIKEY", apikey
objHTTP.setRequestHeader "X-BFX-PAYLOAD", payload
objHTTP.setRequestHeader "X-BFX-SIGNATURE", signature
objHTTP.Send (body)

objHTTP.WaitForResponse
PrivateBitfinex = objHTTP.ResponseText
Set objHTTP = Nothing

End Function

该函数始终抛出错误{" message":"无效的X-BFX-SIGNATURE。"} 你能帮帮我吗?

谢谢

0 个答案:

没有答案