VBA EXCEL API REQUEST - excel

I am trying to make a API request in VBA. I do not know if this is even possible.
Bassically I want to get the latitude and longitude of multiple trucks.
This is what I tried and received error 500:
Sub MakeSOAPRequest()
' Import the MSXML library
Dim xmlhttp As New MSXML2.XMLHTTP60
' Set the request headers
xmlhttp.Open "POST", "https://new.trackgps.ro/TrackGPS.WCFService/TrackGPSWs.svc", False
xmlhttp.SetRequestHeader "Content-Type", "application/soap+xml;charset=UTF-8;action=""http://tempuri.org/ITrackGPS/GetCarriersLocation"""
xmlhttp.SetRequestHeader "Accept-Encoding", "gzip,deflate"
xmlhttp.SetRequestHeader "User-Agent", "Apache-HttpClient/4.1.1 (VBA 1.0)"
xmlhttp.SetRequestHeader "Connection", "Keep-Alive"
' Build the SOAP request body
Dim soapRequest As String
soapRequest = "<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:tem=""http://tempuri.org/"">" & _
"<soap:Header/>" & _
"<soap:Body>" & _
"<tem:GetCarriersLocation>" & _
"<tem:username>USERNAME</tem:username>" & _
"<tem:password>PASSWORD</tem:password>" & _
"</tem:GetCarriersLocation>" & _
"</soap:Body>" & _
"</soap:Envelope>"
' Send the request
xmlhttp.Send soapRequest
' Check for a successful response
If xmlhttp.Status = 200 Then
' Get the response
Dim soapResponse As String
soapResponse = xmlhttp.responseText
' Do something with the response
Debug.Print soapResponse
Else
' Handle an error
MsgBox "Error: " & xmlhttp.Status & " - " & xmlhttp.StatusText
End If
End Sub
These are the instructions I received from my provider
Exemplu de acces:
Request:
POST https://new.trackgps.ro/TrackGPS.WCFService/TrackGPSWs.svc HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type:
application/soap+xml;charset=UTF-8;action="http://tempuri.org/ITrackGPS/GetCarriersLocation"
Content-Length: 483
Host: new.trackgps.ro
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:tem="http://tempuri.org/"> <soap:Header/> <soap:Body>
<tem:GetCarriersLocation> <tem:username>username </tem:username>
<tem:password>PASSWORD</tem:password>
</tem:GetCarriersLocation>
</soap:Body>
</soap:Envelope>
Is there a way I can make a API request using the instructions they gave me?

Related

POST request error 403 Microsoft graph from Excel VBA

I am trying to create folder on sharepoint site with Microsoft graph from Excel VBA, code working on some endpoints but on some it return response error 403. On App permission side, everything looks fine and all permissions are granted. Please look at code below:
Sub create_folder()
Dim http As New MSXML2.XMLHTTP60
Dim url As String
access_token = "tokentokentoken"
url = "https://graph.microsoft.com/v1.0/drives/{drive-id}/items/"
http.Open "POST", url, False
http.setRequestHeader "application", "x-www-form-urlencoded"
http.setRequestHeader "Content-Type", "application/json;odata=verbose"
http.setRequestHeader "Authorization", "Bearer " & access_token
http.send "{""name"": ""New Folder"", ""folder"": {}}"
If http.Status = 200 Then
MsgBox ("OK")
End If
Set XMLHTTP = Nothing
End Sub

Calling an API from Excel Macro to POST data using WinHTTPRequest

I am trying to call an API for sending SMS from Excel Macro.
I tried sending using MSXML2.XMLHTTP it sends SMS successfully from my system, But in client's system it is giving error "An error occurred in the secure channel support".
Set httpObject = CreateObject("MSXML2.XMLHTTP")
sURL = "https://api.xxx.in/send/?apiKey=" & sAPIKey & "&sender=" & sSenderID & "&numbers=" & sMobileNo & "&message=" & sMsg
httpObject.Open "POST", sURL, False
httpObject.Send
sGetResult = httpObject.ResponseText
I tried second method like
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
myURL = "https://api.xxx.in/send/?"
postData = "apikey=" & sAPIKey & "&message=" & sMsg & "&numbers=" & sMobileNo & "&sender=" & sSenderID
winHttpReq.Open "POST", myURL, False
winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
winHttpReq.Send (postData)
SendSMS = winHttpReq.ResponseText
On line winHttpReq.SetRequestHeader it gives error "An error occured in the secure channel support" on my system. As per suggestions I enabled all TLS versions but still the same error. I am using Win 7 and excel 6.5.Somewhere i read to add the ciphers to the servers. I have no idea how to do that. I guess issue is somewhere else.
Can anyone pls help. I will prefer to use the 2nd method.
Thanks

VBA httpWebRequest with Credentials

I need to get some data from a cloud service.
This is my code so far:
Option Explicit
Sub Test()
Call GetHTTPResult("https://venice.unit4.com/WebConnect/api/ZZKlesserRob/2017/Balance/GetBalance?AccountNumber=604&BeginMonth=1&EndMonth=1", "username", "password")
End Sub
Function GetHTTPResult(sURL As String, Optional username As String, Optional password As String) As String
Dim XMLHTTP As Object, sResult As String
Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
username = "dummy"
password = "dummypass"
XMLHTTP.Open "GET", sURL, False
XMLHTTP.setRequestHeader "Authorization", "Basic" & Base64Encode("username" & ":" & "password")
XMLHTTP.Send
Debug.Print "Status: " & XMLHTTP.Status & " - " & XMLHTTP.StatusText
sResult = XMLHTTP.ResponseText
GetHTTPResult = sResult
Set XMLHTTP = Nothing
End Function
Without the credentials it gives me the "401 - Unauthorized" -response, which is good, I suppose.
However I do have the necessary credentials at my disposal but I can't seem to fix the problem posed by the Base64 encoding.
What should I do?
Thanks in advance!
From inspecting the docs I believe your are missing a space in your Authorization header:
var request = new XMLHttpRequest();
...
request.setRequestHeader("Accept","application/xml");
request.setRequestHeader("Authorization","Basic " + Base64.encode ("User:Password"));
So your should read:
XMLHTTP.setRequestHeader "Authorization", "Basic " & Base64Encode(username & ":" & password)
Also, you should add an Accept header to the request with application/xml or application/json to specify the format of the requested data.

Post Request to CoinFloor API in Excel VBA not working - Getting 415 Unsupported Media Type Error

I am trying to access CoinFloor BIST API through Excel VBA or Access VBA. I am able to make get requests (like ticker) successfully. But when I try a post request (like get balance) I get an error "415 Unsupported Media Type". I looked through many threads for this error and all are pointing to the content-type and accept headers, but I was not able to get it working.
The code is pasted below.
Function BalancePostRequest()
Dim auth, URL, data
Dim cred As Credentials
Dim Lib As New jsonlib
cred = getCredentials
auth = cred.userId & "/" & cred.API_Key & ":" & cred.passphrase
URL = "https://webapi.coinfloor.co.uk:8090/bist/XBT/GBP/balance/"
Dim xhr: Set xhr = CreateObject("MSXML2.XMLHTTP")
With xhr
.Open "POST", URL, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "Authorization", "Basic " + Base64Encode(auth)
End With
xhr.send data
If xhr.Status <> 200 Then
Err.Raise vbObjectError + 1000, "Util.SendRequest", "Status code: " & xhr.Status & " " & xhr.statusText & ". Response was: " & xhr.responseText
End If
Set xhr = Nothing
End Function

DocuSign VBA Error 400 errorCode: ENVELOPE_IS_INCOMPLETE

You help would be greatly appreciated to understand what then .setRequestHeader setting should be for this error.
Public Sub SignatureReq()
Dim httpRequest As MSXML2.XMLHTTP60
' defined request and result variables
Set httpRequest = New XMLHTTP60
' define the new URL for the signature request using the Base URL returned aboved appended with '/envelopes'
Dim sigURL As String
sigURL = URLBase & "/envelopes"
' currently sig URL is 'https://demo.docusign.net/restapi/v2/accounts/[account number]'
Dim Body As String
Body = "<envelopeDefinition xmlns=\" & Chr(34) & "http://www.docusign.com/restapi\" & Chr(34) & ">" + _
"<accountId>MyAccountID</accountId>" + _
"<status>sent</status>" + _
"<emailSubject>DocuSign API Call - Signature request from template</emailSubject>" + _
"<templateId>MyTemplateID</templateId>" + _
"<templateRoles>" + _
"<templateRole>" + _
"<name>Ronco</name>" + _
"<email>rolsen#myemail.com</email>" + _
"<roleName>Rono</roleName>" + _
"</templateRole>" + _
"</templateRoles>" + _
"</envelopeDefinition>"
Debug.Print Body
httpRequest.Open "POST", sigURL, False
httpRequest.setRequestHeader Body, "application/xml"
httpRequest.setRequestHeader "X-DocuSign-Authentication", Author
'httpRequest.setRequestHeader "Accept", "application/xml"
httpRequest.setRequestHeader "Content-Type", "multipart/form-data"
'httpRequest.setRequestHeader "Content-Type", "application/xml"
httpRequest.send
Debug.Print httpRequest.Status, "A"
Debug.Print httpRequest.statusText, "B"
Debug.Print httpRequest.responseText, "C"
Exit Sub
The response message is below. I understand the error is coming from the .setRequestHeader not being set correctly. Where can I find how the header and values are needed to be set for each .setRequestHeader that DocuSign will accept?
400 Bad Request { "errorCode":
"ENVELOPE_IS_INCOMPLETE", "message": "The Envelope is not Complete.
A Complete Envelope Requires Documents, Recipients, Tabs, and a
Subject Line. Content-Type does not contain boundary parameter." }
When sending a signature request from a Template the Content-Type for the API call needs to be set to either application/json or application/xml. It looks like you've currently got that commented out and are sending a multipart/form-data request. Comment that line out and add back in:
httpRequest.setRequestHeader "Content-Type", "application/xml"
If that doesn't work or you've already tried that I recommend enabling Request Logging through your developer account Preferences and examining the raw request that's being set to make sure your code is generating what you expect.

Resources