Exchanges

Binance API SIGNED GET 請求錯誤程式碼 -1100 上的簽名參數

  • March 15, 2021

我正在嘗試將我的加密投資組合載入到 Excel 中,以便在我的交易中使用其簡單的分析功能。但是,我無法使用 VBA 的 WinHttpRequest 連接到 API。

我使用以下程式碼嘗試獲取帳戶資訊(餘額)/api/v3/account

Sub GetBalances()
Dim tNow As String
tNow = CStr(Date2Long(Now()))
Dim sUrl As String
sUrl = "https://api.binance.com/api/v3/account?timestamp=" & tNow & "&signature=[signature here]"

Dim oRequest As WinHttp.WinHttpRequest
Dim sResult As String

On Error GoTo Err_DoSomeJob

Set oRequest = New WinHttp.WinHttpRequest
With oRequest
   .Open "GET", sUrl, True
   .SetRequestHeader "Content-Type", "application/json; charset=UTF-8"
   .SetRequestHeader "X-MBX-APIKEY", "[API key here]"
   .Send "{}"
   .WaitForResponse
   sResult = .ResponseText
   Debug.Print sResult
   sResult = oRequest.Status
   Debug.Print sResult
End With


Exit_DoSomeJob:
   On Error Resume Next
   Set oRequest = Nothing
   Exit Sub

Err_DoSomeJob:
   MsgBox Err.Description, vbExclamation, Err.Number
   Resume Exit_DoSomeJob


End Sub

Private Function Date2Long(dtmDate As Date) As Long
   Date2Long = (dtmDate - #1/1/1970#) * 86400
   Exit Function
End Function

但是它給出了以下錯誤消息:

{“code”:-1100,“msg”:“參數’signature’中發現非法字元;合法範圍是’^[A-Fa-f0-9]{64}$’。”}

這似乎很奇怪,因為密鑰的字元範圍只有 AF,而提供的密鑰似乎包含 AZ。還有其他人知道如何解決這個問題嗎?可能是VBA引起的問題?或者問題可能出在其他地方?

根據@Marc Alexander 的評論,我必須簽署(HMAC SHA256)查詢參數(在我的情況下timestamp=##################)並將其作為簽名參數添加到請求中。它使用以下程式碼工作:

Sub GetBalances()
   Const apiKey As String = "[insert API key]"
   Const secret As String = "[insert API secret]"

   Dim totalParams As String
   totalParams = "timestamp=" & BinanceTime()
   Signature = Base64_HMACSHA256(totalParams, secret)
   sUrl = "https://api.binance.com/api/v3/account?" & totalParams & "&signature=" & Signature
   Dim oRequest As WinHttp.WinHttpRequest
   Dim sResult As String

   On Error GoTo Err_DoSomeJob

   Set oRequest = New WinHttp.WinHttpRequest
   With oRequest
       .Open "GET", sUrl, True
       .SetRequestHeader "Content-Type", "application/json; charset=UTF-8"
       .SetRequestHeader "X-MBX-APIKEY", apiKey
       .Send "{}"
       .WaitForResponse
       sResult = .ResponseText
   End With
   Set JsonResponse = JsonConverter.ParseJson(sResult)
   Debug.Print(

Exit_DoSomeJob:
   On Error Resume Next
   Set oRequest = Nothing
   Exit Sub

Err_DoSomeJob:
   MsgBox Err.Description, vbExclamation, Err.Number
   Resume Exit_DoSomeJob


End Sub

Public Function BinanceTime() As String
   Dim oRequest As WinHttp.WinHttpRequest
   Dim sResult As String

   Set oRequest = New WinHttp.WinHttpRequest
   With oRequest
       .Open "GET", "https://api.binance.com/api/v1/time", True
       .SetRequestHeader "Content-Type", "application/json; charset=UTF-8"
       .Send "{}"
       .WaitForResponse
       sResult = CStr(.ResponseText)
   End With
   Set Json = JsonConverter.ParseJson(sResult)
   BinanceTime = Json("serverTime")
End Function

Public Function Base64_HMACSHA256(ByVal sTextToHash As String, ByVal sSharedSecretKey As String)
   Dim asc As Object, enc As Object

   Dim TextToHash() As Byte
   Dim SharedSecretKey() As Byte
   Set asc = CreateObject("System.Text.UTF8Encoding")
   Set enc = CreateObject("System.Security.Cryptography.HMACSHA256")

   TextToHash = asc.Getbytes_4(sTextToHash)
   SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
   enc.Key = SharedSecretKey

   Dim Bytes() As Byte
   Bytes = enc.ComputeHash_2(TextToHash)
   Base64_HMACSHA256 = ByteArrayToHex(Bytes)
   Set asc = Nothing
   Set enc = Nothing

End Function

Private Function ByteArrayToHex(ByRef ByteArray() As Byte) As String
   Dim l As Long, strRet As String

   For l = LBound(ByteArray) To UBound(ByteArray)
       'strRet = strRet & Hex$(ByteArray(l))
       strRet = strRet & WorksheetFunction.Dec2Hex(ByteArray(l), 2)
       Debug.Assert l = Len(strRet) / 2 - 1
   Next l
   ByteArrayToHex = strRet
End Function

引用自:https://bitcoin.stackexchange.com/questions/67365