Anyone have a macro to download sales from https://go.unleashedsoftware.com/v2.

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I'm really good at editing code normally, but no good at just creating it,
I need to connect to the API of Log In - Unleashed Software - Online Inventory Software.
I was thinking I can't be the very first person to do this so maybe someone has some code they could send me that i can use and adapt if need be>
Any help on how to put this together any help in any way would be greatly appreciated, and if some wants to put an example version together for me then even greater
Thanks

Tony
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You can use Unleashed API to access resources, such as Customers, Products, and Purchase Orders, etc. Full documentation can be found here.
I have written a sample VBA code that you can download here: UnleashedAPI.xlsb.
Hope it helps.
 
Upvote 0
Hi nguyendang,
thats very helpfull but I cant access the document, would you mind either ppost the code here, or letting me access it some otherway please.
Thanks
Tony
 
Upvote 0
Hi nguyendang,
thats very helpfull but I cant access the document, would you mind either ppost the code here, or letting me access it some otherway please.
Thanks
Tony
As you requested.
VBA Code:
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
 
Upvote 0
Thank you very much this is great, i'll get playing with it now :)
 
Upvote 0
OK, great news it looks like its worked, but its only coming into the imidiate window
how do i get it to load to activesheet?

please help i'm so close i can fel it
 
Upvote 0
OK, great news it looks like its worked, but its only coming into the imidiate window
how do i get it to load to activesheet?

please help i'm so close i can fel it
How about saving the response as JSON/XML text file to a folder then using Power Query to load data to spreadsheet? It is much easier to parse JSON/XML in Power Query than in VBA.

VBA Code:
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
            On Error GoTo 0
            If .Status = 200 Then
                Dim objFSO As Scripting.FileSystemObject
                Set objFSO = New Scripting.FileSystemObject
                Dim objTextStream As Scripting.TextStream
                Set objTextStream = objFSO.CreateTextFile("Z:\UnleashedAPI\SalesOrders.xml", True, True) 'Save the response as XML to Z:\UnleashedAPI
                objTextStream.Write .ResponseText
                objTextStream.Close
            End If
        Else
            MsgBox Err.Description, vbExclamation, "Error"
        End If
    End With
End Sub

1694272597633.png


1694272715559.png
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,617
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top