I apologize if my question's title is incorrect - I'm used to the idea of PHP sessions with APIs.
I'm trying to accomplish the same feat in VBA with the following code:
'Login
strLogin = "https://URL.COM/authenticateUser?login=username&apiKey=password"
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlHttp.Open "GET", strLogin
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
'Save the response to a string
strReturn = xmlHttp.responseText
'Open URL and get JSON data
strUrl = "https://URL.COM/Search/search?searchTerm=" & Keyword & "&mode=beginwith"
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlHttp.Open "GET", strUrl
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
'Save the response to a string
strReturn = xmlHttp.responseText
Sheets(1).Cells(20, 2).Value = strReturn
With this API, I need to login first prior to executing any calls that will return data.
My problem is that I cannot determine how to "stay logged in" so that my second call works.
after I login, strReturn is populated with the following string:
{"Status":{"Code":"2","Message":"Authentication Succeeded","Success":"true"}}
However, when I go to utilize strUrl, I get the following message:
{"Status":{"Code":"1","Message":"Invalid User Name Or Password","Success":"false"}}
I have used this code in prior projects where I needed to supply an API key along with the URL for each request to the server - so this obviously worked fine. I'm not sure how to achieve the concept of "establishing a session" though with xmlHttp.
So, to anyone else who runs across this, the simple solution was to remove the following line from the second call:
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
By not creating a new object, vba is able to keep and use the cookie from the first login call.
The final code looks like this:
'Login
strLogin = "https://URL.COM/authenticateUser?login=username&apiKey=password"
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlHttp.Open "GET", strLogin
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
'Save the response to a string
strReturn = xmlHttp.responseText
'Open URL and get JSON data
strUrl = "https://URL.COM/Search/search?searchTerm=" & Keyword & "&mode=beginwith"
xmlHttp.Open "GET", strUrl
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
'Save the response to a string
strReturn = xmlHttp.responseText
Sheets(1).Cells(20, 2).Value = strReturn
And the API which requires a login is able to keep the session established.
Related
I am fetching data from website. It was working my laptop till last week without any trouble anytime, All of the sudden, it started to give strange behavior(It works in evening and it fail to work during day time. Can anyone please look into below sample code and let me know the reason?
Sub FailedTest2()
Dim xmlhttp As Object
'Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0") ' I tried this also
URL = "http://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=HDFCBANK&instrument=FUTSTK&expiry=26DEC2019"
xmlhttp.Open "GET", URL, False
xmlhttp.setRequestHeader "Content-Type", "text/JSON"
xmlhttp.Send
MsgBox xmlhttp.responseText
End Sub
I'm trying to check a webpage if it is logged in or not. For that I used win http GET request for google to test it. But i'm getting
"The Operation Timed Out" Error
after Send command.
Also Tried with xmlHTTP and serverxmlHTTP.
Dim xmlHTTP As Object
Dim html As Object
Dim url As String
url = "https://www.google.com/"
'
'Set xmlHTTP = CreateObject("MSXML2.XMLHTTP")
Set xmlHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
xmlHTTP.SetTimeouts 10000, 10000, 10000, 10000
xmlHTTP.Open "GET", url, False
'xmlHTTP.setRequestHeader "Content-Type", "text/xml"
xmlHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = xmlHTTP.responseText
"The Operation Timed Out" Error after Send command.
I'm working on a API integration on my Excel WorkSheet using VBA and I'm trying to send user input on a cell sent to a RequestHeader in order to find a product on the server
I've tried importing the data directly from the sheet onto a string and then sending that string as the value for the header but I can't seem to get it to work. I'm new to this whole VBA stuff so I don't honestly know if that's possible. Here's what I've tried:
Sub GetCompanyInformation()
Dim hReq As Object, JSON As Dictionary
Dim i As Long
Dim var As Variant
Dim ws As Worksheet
Set ws = Sheet1
'create our URL string and pass the user entered information to it
Dim strUrl As String
strUrl = "My API URL Goes Here"
Dim cod As String
cod = ws.[MYCELL]
Set hReq = CreateObject("MSXML2.XMLHTTP")
With hReq
.Open "GET", strUrl, False
.SetRequestHeader "cache-control", "no-cache"
.SetRequestHeader "data_filter", ""
.SetRequestHeader "data", "products"
.SetRequestHeader "data_id", cod
.Send
End With
[...]
It returns me an empty string. However if I send the request with the same number I add on my cell as such:
[...]
Set hReq = CreateObject("MSXML2.XMLHTTP")
With hReq
.Open "GET", strUrl, False
.SetRequestHeader "cache-control", "no-cache"
.SetRequestHeader "data_filter", ""
.SetRequestHeader "data", "products"
.SetRequestHeader "data_id", "1234"
.Send
End With
It Works, and it returns me the data I need. Am I doing something wrong? Can I get the variable data on a Request Header?
I am trying to use Excel-VBA to perform an https POST of an XML body as follows:
Public Sub test()
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
Dim myDom As Object
Set myDom = CreateObject("MSXML2.DOMDocument.6.0")
URL = "https://uatgateway.topl.com.au/quote"
myDom.async = False
myXMLstr = URL & "<?xml version=&Chr(34)&1.0&Chr(34)& encoding=&Chr(34)&UTF-
8&Chr(34)& standalone=&Chr(34)&yes&Chr(34)&?><IMPULSE><REQUEST><QUOTE>
<MERCHANT><ACQUIRERID>Bank A</ACQUIRERID><NETWORKID>eComm</NETWORKID>
<MERCHANTID>987654321</MERCHANTID></MERCHANT><MCH_TRANSACTION>
<QUOTEREF>3456789012</QUOTEREF><MCH_ISO>036</MCH_ISO>
<MCH_AMT>1000.00</MCH_AMT><BIN>400555000</BIN></MCH_TRANSACTION></QUOTE>
</REQUEST></IMPULSE>"
myDom.Load (myXMLstr)
xmlhttp.Open "post", URL, False
xmlhttp.setRequestHeader "Content-Type", "application/xml"
xmlhttp.setRequestHeader "Content-Length", "356"
xmlhttp.setRequestHeader "Accept", "text/xml"
xmlhttp.setRequestHeader "Host", "uatgateway.topl.com.au"
xmlhttp.setRequestHeader "Cache-Control", "no-cache"
xmlhttp.send myDom.XML
MsgBox xmlhttp.responseText
End Sub
My VBA code compiles ok, but on execution stops at the xmlhttp.send line almost immediately with an error "the server returned an invalid or unrecognised response". The same POST on Postman works. I am not sure if I an referencing the myDom.XML correctly?
Any suggestions welcome?
I'm trying to get data from one report with VBA in Excel.
I get this error message:
{"data":{"message":"Request method 'GET' not supported","code":3000}}
This is my code:
Dim strUrl As String
strUrl = "https://api.clockify.me/api/workspaces/{workspaceId}/reports/{reportId}/"
Set hReq = CreateObject("MSXML2.XMLHTTP")
With hReq
.Open "GET", strUrl, False
.SetRequestHeader "X-api-key", "{api-key}"
.SetRequestHeader "content-type", "application/json"
.Send
End With
'wrap the response in a JSON root tag "data" to count returned objects
Dim response As String
response = "{""data"":" & hReq.ResponseText & "}"
Debug.Print response
Why is the GET method not allowed here?
Looking at the documentation (https://clockify.github.io/clockify_api_docs/#tag-Report)... I don't see a "GET /workspaces/{workspaceId}/reports/{reportId}/" API call - the closes is either GET /reports/{reportId} or PUT/DELETE /workspaces/{workspaceId}/reports/{reportId}, which would explain why you are getting the "GET not allowed", because you can only PUT or DELETE to the /workspaces/{workspaceId}/reports/{reportId}/ endpoint. I'm guessing you want the "GET /reports/{reportId}". Try that.
And I'm guessing this GET /reports/{reportId} isn't under workspaces because reports can be made public... but that's just a guess.