VBA Kerberos Authentication - vba

I want to send a POST request to a Java servlet with VBA using the Kerberos ticket from the Windows Log In for authentication and then retrieve a JSON response from the servlet. Can I achieve this without using an InternetExplorer object, e.g. using WinHttpRequest?

WinHttpRequest is not an IE object. The above described usecase works perfectly as long as you set up to relax the security permissions. I have implemented a proof of concept last year: Excel, VBA, REST, Tomcat, SPNEGO authentication.
Stub code:
Dim winHttp As Object
Set winHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
winHttp.SetAutoLogonPolicy (0)
winHttp.Open "GET", "http://..."
winHttp.send
Dim success As Boolean
success = winHttp.waitForResponse(5)
If Not success Then
Debug.Print "DOWNLOAD FAILED!"
Exit Sub
End If
Dim responseText As String
responseText = winHttp.responseText

Related

Posting with an open event

Action: Trying to 'post' excel data to a webpage that the user sees/uses with a vba code event.
Issue: I can open a window with a get, or I can xmlhttp with a post and a get a response variable, but neither is what I need. I need to POST login information (a service account) from baked in vba code on a button to POST and open a browser.
The webpage is behind Spring Security and the service account credentials should not be known to the user, it's hidden in a protected workbook/vba. I need to post those over to the url.
--> How do I open AND post at the same time?
What I need is the excel equivalent of an HTML form post with a
target=_blank
attribute in vba. Is this possible?
I've tried both
ShellExecute
functions
and
xmlhttp
methods
but both only give me half of the package.
Any advice?
Adding to what #Florent B. has posted, check whether the cookie is set when calling the login page (which I assume) or really when posting the login data.
Dim xhttp As MSXML2.XMLHTTP ' make sure you reference to MSXML!
Dim strCookie As String
Set xHttp = New MSXML2.XMLHTTP
XMLHttp.Open "GET" TheURLOfYourLogInPage
If xHttp.Status = 200 Then
strCookie = xHttp.getResponseHeader("Set-Cookie")
End If
Check your web browser for what is sent to the server when login in, probably something like "username=" & YourUserName & "&password=" & YourPassword & "&cookie=" & strCookie Declare another string variable (I will call it strTicker) and fill it accordingly, then
xHttp.Open "POST", "URLOfYourLoginPage"
xHttp.setRequestHeader "Cookie", strCookie
xHttp.send strTicker

Big Commerce API Put call inside VBA

I have an excel sheet which is doing some matching from Quickbooks (hence the Excel).
I know I can pass "PUT" and "GET" calls using DOM and XMLHTTP but when I send the PUT I get the message back:
A content type for the input was not supplied.
Is this a syntax issue on my part? I've tried a few ways:
/api/v2/products/33288?"Content-Type":"application/json"&"condition":"Worn"
/api/v2/products/33288?"condition":"Worn"
/api/v2/products/33288,"condition":"Worn"
All with similar results.
If it doesn't show, I am not all that comfortable with API calls, but very comfortable with VBA
Full Code
Dim req As New XMLHTTP
Dim doc As New DOMDocument
Dim url As String
Dim nd As String
url = "https://USER:PASS#WEBSITE/api/v2/products/33288?""Content-Type"":""application/json""&""condition"":""Worn"""
req.Open "PUT", url, False
req.Send
doc.LoadXML req.ResponseText
doc.Save "C:\Users\Admin\Desktop\WebQueryResult2.xml"

Please enable cookies message (httpwebrequest)

I'm trying to send a request to a website but it keeps telling me my cookies are not enabled. (But i did used a cookiecontainer in my code)
Here is the code :
Private Function does_it_asks_for_cookies() As Boolean
Dim url As String = "https://www.myhabit.com/?hash="
Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(URL)
req.CookieContainer = New CookieContainer
req.ContentType = "application/x-www-form-urlencoded"
Dim res As Net.HttpWebResponse = req.GetResponse()
Dim strm As New IO.StreamReader(res.GetResponseStream())
Dim html As String = strm.ReadToEnd()
strm.Dispose()
res.Dispose()
If html.Contains("To continue shopping at Amazon.com, please enable cookies in") Then
Return True
Else
Return False
End If
End Function
Usage : MsgBox(does_it_asks_for_cookies)
While you have a CookieContainer the code in your example doesn't actually put anything in it. HttpWebRequest doesn't know anything about previous requests and certainly doesn't magically store the contents of any cookies in your container, you will have to do that yourself. Having a CookieContainer isn't enough. Most (especially e-commerce) websites are very specific in what they expect there to be present in the form of a cookie, often reporting that cookies aren't turned on when the cookie doesn't contain the proper values. You will have to inspect the web site in a browser to figure out what cookies are set and what values they contain. Google "inspect cookies" to learn how to view cookies in the browser of your choice.

VB.NET (WebRequest Authentication Issue)

I'm new to WebRequest authentication and have been researching how to authenticate w/ a couple websites to pull some excel data from them. Couple things I'm confused about is
a.) how to properly read a log from Fiddler (using this to pick up get/post data from the website authentication)
b.) how do use the data from Fiddler to program the VB.NET WebRequest properly.
I've been able to authenticate w/ websites that use simple authentication HTTPS, but any site that does any redirects/REST/cookie auth I'm lost...
Let me know if I can provide anymore detail.
Dim req As Net.HttpWebRequest = Net.WebRequest.Create(Url)
If Not Login = Nothing AndAlso Not Password = Nothing Then
Dim myCache As New System.Net.CredentialCache()
myCache.Add(New Uri(Url), "Basic", New System.Net.NetworkCredential(Login, Password))
req.Credentials = myCache
End If
Dim sr As New StreamReader(req.GetResponse().GetResponseStream())
Dim ss as string = sr.ReadToEnd
'Save it as excel & close stream
sr.Close()

Sending HTTP requests with VBA from Word

I am trying to send data from a Word document to a web page. I have found some code, pasted it into a new module, and saved it. When I run it I get "compile error, user defined type not defined"
My code:
Sub http()
Dim MyRequest As New WinHttpRequest
MyRequest.Open "GET", _
"http://www.google.com"
' Send Request.
MyRequest.Send
'And we get this response
MsgBox MyRequest.ResponseText
End Sub
A potential alternative to avoid having to select the library is to use an object i.e.
Sub http()
Dim MyRequest As Object
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "GET", _
"http://www.google.com"
' Send Request.
MyRequest.Send
'And we get this response
MsgBox MyRequest.ResponseText
End Sub
You need to set a reference to Microsoft WinHTTP Services in your VBA Project (Tools -> References).
Here's what it would look like:
Also, you can read more about the Microsoft WinHTTP Services, version 5.1 here.
You will need to change your references (Tools=>References in the code window). Look for Microsoft WinHTTP Services, version 5.1 (or newer) and tick the box. If you are using Vista and office 2007, you may also need to register it first. Open a command window as administrartor and paste:
>regsvr32.exe "c:\windows\system32\winhttp.dll"
It should say if it works.