Dim domain: domain = "https://app.vinsight.net"
Dim username: username= "abc@example.com"
Dim password: password = "mypassword"
Public Function login(username, password)
Dim encodedCredentials: encodedCredentials = Replace(EncodeBase64(URLEncode(username, False) & ":" & URLEncode(password, False)), vbLf, "")
Dim xmlHttp, URL, async: async = False
Dim userAgent: userAgent = "Microsoft.XMLHTTP"
Set xmlHttp = CreateObject(userAgent)
URL = domain & "/LoginContexts?$format=json"
xmlHttp.Open "POST", URL, async
xmlHttp.setRequestHeader "User-Agent", userAgent
xmlHttp.setRequestHeader "Authorization", "Basic " & encodedCredentials
xmlHttp.setRequestHeader "Host", "app.vinsight.net"
xmlHttp.setRequestHeader "Content-Length", "0"
xmlHttp.send
If xmlHttp.Status = 200 Then
Set Login = xmlHttp
Else
Throw
End If
End Function
Function encodeBase64(text)
Dim objNode, objXML: Set objXML = CreateObject("MSXML2.DOMDocument")
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = stringToBinary(text)
EncodeBase64 = objNode.text
Set objNode = Nothing
Set objXML = Nothing
End Function
Public Function URLEncode(StringVal, SpaceAsPlus)
Dim StringLen: StringLen = Len(StringVal)
If StringLen > 0 Then
ReDim result(StringLen)
Dim i, CharCode
Dim Char, Space
If SpaceAsPlus Then Space = "+" Else Space = "%20"
For i = 1 To StringLen
Char = Mid(StringVal, i, 1)
CharCode = Asc(Char)
If (CharCode >= 97 And CharCode <= 122) _
Or (CharCode >= 65 And CharCode <= 90) _
Or (CharCode >= 48 And CharCode <= 57) _
Or CharCode = 45 Or CharCode = 46 Or CharCode = 95 Or CharCode = 126 Then
result(i) = Char
ElseIf CharCode = 32 Then
result(i) = Space
ElseIf CharCode >= 0 And CharCode <= 15 Then
result(i) = "%0" & Hex(CharCode)
Else
result(i) = "%" & Hex(CharCode)
End If
Next
URLEncode = Join(result, "")
End If
End Function
Function stringToBinary(Text)
Const adTypeText = 2
Const adTypeBinary = 1
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeText
BinaryStream.CharSet = "us-ascii"
BinaryStream.Open
BinaryStream.WriteText Text
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary
BinaryStream.Position = 0
stringToBinary = BinaryStream.Read
Set BinaryStream = Nothing
End Function
Function getHeaders(xmlHttp, headerName)
headerName = Trim(headerName) & ":"
Dim tArr: tArr = Split(xmlHttp.getAllResponseHeaders(), vbCrLf)
tArr = Filter(tArr, headerName, True, vbTextCompare)
Dim i
For i = 0 To UBound(tArr)
tArr(i) = Trim(Mid(tArr(i), Len(headerName) + 1, Len(tArr(i))))
Next
getHeaders = tArr
End Function
Public Function getCookies(xmlHttp)
Dim headers: headers = getHeaders(xmlHttp, "Set-Cookie")
Dim cookies: Set cookies = CreateObject("Scripting.Dictionary")
Dim i, name, value, delimiterPos
For i = 0 To UBound(headers)
delimiterPos = InStr(headers(i), "=")
name = Left(headers(i), delimiterPos - 1)
value = Split(Mid(headers(i), delimiterPos + 1), ";")(0)
cookies.Add name, value
Next
Set getCookies = cookies
End Function
Public Function getJson(URL, cookies)
Set getJson = getAs(URL, cookies, "application/json")
End Function
Public Function getAs(URL, cookies, acceptType)
Dim authCookieNames(1)
authCookieNames(0) = "ASP.NET_SessionId"
authCookieNames(1) = ".ASPXAUTH"
Dim userAgent: userAgent = "Microsoft.XMLHTTP"
Dim xmlHttp: Set xmlHttp = CreateObject(userAgent)
Dim async: async = False
xmlHttp.Open "GET", domain & URL, async
xmlHttp.setRequestHeader "User-Agent", userAgent
xmlHttp.setRequestHeader "Accept", acceptType
xmlHttp.setRequestHeader "Host", "app.vinsight.net"
xmlHttp.setRequestHeader "Content-Length", "0"
Dim i, cookie
For i = 0 To UBound(authCookieNames)
cookie = authCookieNames(i) & "=" & cookies(authCookieNames(i)) & "; "
Next
xmlHttp.setRequestHeader "Cookie", cookie
xmlHttp.send
Set getAs = xmlHttp
End Function
Function test(URL)
Dim loginRequest: Set loginRequest = login(username, password)
Dim cookies: Set cookies = getCookies(loginRequest)
Dim resp: Set resp = getAs(URL, cookies, "text/csv")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
stdout.WriteLine "Status: " & resp.Status
stdout.WriteLine "ResponseText: " & resp.responseText
End Function
test "/Vessels?$select=VesselCode,Volume,BatchCode,BatchStatus&$top=999"