Option Explicit
Public Sub GetFirst200SalesOrders()
Const APIId As String = "API_ID" 'API Id, get it from https://au.unleashedsoftware.com/v2/Integration/Api
Const APIKey As String = "API_KEY" 'API Key, get it from https://au.unleashedsoftware.com/v2/Integration/Api
Dim objWinHttp As WinHttp.WinHttpRequest
Set objWinHttp = New WinHttp.WinHttpRequest
With objWinHttp
.Open "GET", "https://api.unleashedsoftware.com/SalesOrders", True
.SetRequestHeader "Accept", "application/xml" 'application/xml = XML, 'application/json = JSON
.SetRequestHeader "Content-Type", "application/xml" 'application/xml = XML, 'application/json = JSON
.SetRequestHeader "api-auth-id", APIId
.SetRequestHeader "api-auth-signature", HMAC("SHA256", "", APIKey)
.SetRequestHeader "client-type", "unleashedapi/excel-vba" 'Optional, for tracking purpose only
.Send
On Error Resume Next
.WaitForResponse
If Err.Number = 0 Then
If .Status = 200 Then
Debug.Print .ResponseText
End If
Else
MsgBox Err.Description, vbExclamation, "Error"
End If
On Error GoTo 0
End With
End Sub
'Credited to https://excelbaby.com/learn/excel-macro-base64-hmac-encryption/
Private Function HMAC(ByVal sType As String, ByVal sTextToHash As String, ByVal sSharedSecretKey As String) As String
'sType: SHA1, SHA256, SHA384, SHA512
Dim asc As Object, enc As Object
Dim TextToHash() As Byte
Dim SharedSecretKey() As Byte
Set asc = CreateObject("System.Text.UTF8Encoding") 'only working in .NET Framework 3.5
sType = UCase(sType)
Select Case sType
Case "SHA1"
Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")
Case "SHA256"
Set enc = CreateObject("System.Security.Cryptography.HMACSHA256")
Case "SHA384"
Set enc = CreateObject("System.Security.Cryptography.HMACSHA384")
Case "SHA512"
Set enc = CreateObject("System.Security.Cryptography.HMACSHA512")
Case Else
HMAC = "Error! sType value: SHA1, SHA256, SHA384, SHA512"
Exit Function
End Select
TextToHash = asc.Getbytes_4(sTextToHash)
SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
enc.Key = SharedSecretKey
Dim bytes() As Byte
bytes = enc.ComputeHash_2((TextToHash))
HMAC = EncodeBase64(bytes)
End Function
'Credited to https://excelbaby.com/learn/excel-macro-base64-hmac-encryption/
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
End Function