(Solved) – VBA script: responseMessage:”The HMAC validation failed : Invalid Signature or no matching alias found


I have been exploring REST api call through VBA scripting. I am just making a simple REST API request. I have followed all the steps mentions and correctly
verified each and everything. Below is the detail per item.

API Key :- I have verified my api key its correct.
API Secret :- It is also correct.

responseMessage:”The HMAC validation failed : Invalid Signature or no
matching alias found

can anyone Please help me on this code.

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 = EncodeBase64(bytes)
    Set asc = Nothing
    Set enc = Nothing
End Function

Private Function EncodeBase64(ByRef arrData() As Byte) As String

    'Inside the VBE, Go to Tools -> References, then Select Microsoft XML, v6.0
    '(or whatever your latest is. This will give you access to the XML Object Library.)

    Dim objXML As MSXML2.DOMDocument60
    Dim objNode As MSXML2.IXMLDOMElement

    Set objXML = New MSXML2.DOMDocument60

    ' byte array to base64
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    EncodeBase64 = objNode.Text

    Set objNode = Nothing
    Set objXML = Nothing

End Function

    Sub Submit()
    Dim sURL As String, sHTML As String, sAllPosts As String
    Dim jsonText As String
    Dim enc As String
    Dim jsonObject As Object
    Dim i As Long
    Dim ws As Worksheet
    Dim oHttp, fsobj, txtobj As Object
    Dim blWSExists As Boolean
    Dim Body, certificate_data, s As String

    Set ws = Worksheets("sheet1")
        l = Len(certificate_data)
        Set oHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
        Set fsobj = CreateObject("Scripting.FileSystemObject")
        sURL = "url"
        jsonText = ws.Cells(1, 1)
        oHttp.Open "POST", sURL, False
        oHttp.setRequestHeader "Content-Type", "application/json"
        oHttp.setRequestHeader "Accept", "application/json"
        enc = Base64_HMACSHA256("sTextToHash", "sSharedSecretKey")
        oHttp.setRequestHeader "hmac-signature", "enc"
        a = Len(certificate_data)
        oHttp.SetClientCertificate ("certificate name")
        oHttp.send (jsonText)
        sHTML = oHttp.responseText`enter code here`
        Worksheets("Sheet1").Range("E1").Value = sHTML
    End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *