default credentials in VBA - vba

I'm tryng to make a HTTP request from VB for Applications ( VBA)
that should have been simple:
URL = "https://www.google.com/"
Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xhr.Open "GET", URL, False
xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xhr.Send
MsgBox xhr.responseText
BUT I'm behind a proxy server which require basic auth (base64).
How to set the credentials in VBA ?
(something equivalent to : myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials)
If this is not possible, how to solve the issue with another approach ?
Thanks !

You'd need the setProxyCredentials method. I don't have a proxy to test, but this should do the trick:
URL = "https://www.google.com/"
Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xhr.Open "GET", URL, False
xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xhr.setProxy 2, "192.168.0.222:8080"
xhr.setProxyCredentials "your_username" , "password_for_username"
xhr.Send
MsgBox xhr.responseText
Taken from This Question which points out the 2 argument is a version number that you may need to change.

Related

Oauth 2 refresh Access token

I try to refresh Access Token with VBScript, but get response : invalid_grant /Bad request
Request:
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
XMLHTTP.Open "POST", "https://accounts.google.com/o/oauth2/token", False
XMLHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
DataToSend = "client_id=266176344773-h49u1s85v2jr1r9fto36ikc8um3cgino.apps.googleusercontent.com&client_secret=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&refresh_token=1//03ysiLvBrl3_VCgYIARAAGAMSNwF-L9IrsdarWW6z8ylRbmNdeMSrynHhLT8utqp0Tlwt6lWCkgQMQ3FCa4OotTU2_hFKCcBNqnA&grant_type=refresh_token"
What's wrong in my request?
Thnks in advance,
Nadia

How to retrieve and store JSESSIONID cookie?

I'm writing a web parser, everything is going well except the JSESSIONid which I cannot retrieve correctly. When I make the request with Postman I obtain a JSESSIONid cookie which I can further force into my VBA code, but that's not what I want. I want to get that cookie directly from my code.
Basically this is the part of my code which causes problem:
WHTTP.Open "POST", mainUrl & RequestAuth, False
WHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.Send strAuthenticate
jsessionIDCookie = WHTTP.GetResponseHeader("Set-Cookie")
jsessionIDCookie = Split(jsessionIDCookie, ";")(0)
I just obtained a jsessionIDCookie from the code, but it doesn't help me at the auth:
WHTTP.Open "POST", mainUrl & RequestAuth, False
WHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.SetRequestHeader "Cookie", jsessionIDCookie
WHTTP.Send strAuthenticate
When I force the jsessionIDCookie (retrieved from Postman) like this:
jsessionIDCookie = "JSESSIONID=lg49wqrAhHAGy5PirUBfBXM6uChBa67cXopvbDdK.i3jbseotaaff2"
Into my code, and ignore the first part more above, the authorization works fine!

Twilio SMS API call not recognizing mediaurls

My goal is to send a text message in VBA with Twilio's API by using WinHttpRequest and a POST call.
I put together a custom call using the WinHttpRequest for a basic SMS message and that worked. I can send text messages just fine.
But then I tried adding the MediaURL to the data, and it ignored the URL (no image attached to SMS). I confirmed the URL was valid by testing it in the Visual Studio project and it was able to send just fine.
I went back and tried sending a test image on twilio's servers and it works. I copied the same image to a server and it won't work.
WORKS
HTTPReq.send ("Body=Hi Brent, Hello From Me!&From=+1XXXXXXXXXX&To=+1XXXXXXXXX&MediaUrl=https://demo.twilio.com/owl.png")
DOESN'T WORK
HTTPReq.send ("Body=Hi Brent, Hello From ME!&From=+1XXXXXXXXXX&To=+1XXXXXXXXXX&MediaURL=https://smarttanktester.com/wp-content/uploads/2018/10/owl.png")
(Phone numbers removed).
I tested with a few more images from their site and they work.
So it appears that URLs external to their server are being ignored. My guess is that I have something configured wrong from my end.
Here is my code.
TargetURL = "https://api.twilio.com/2010-04-01/Accounts/XXX/Messages"
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
HTTPReq.Open "POST", TargetURL, False
HTTPReq.setRequestHeader "Accept", "application/json"
temp = "Basic " & EncodeBase64
HTTPReq.setRequestHeader "Authorization", temp
HTTPReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
HTTPReq.send ("Body=Hi Brent, Hello From Me!&From=+1XXXXXXXXXX&To=+1XXXXXXXXXX&MediaURL=https://smarttanktester.com/wp-content/uploads/2018/10/owl.png")
Any help is greatly appreciated.

Can't replicate successful HTTP Request in VBA; Returns 403 Error when made in VBA, but not cURL

This curl request works and gives a 200 response code when made through the windows command line:
curl -v -u user:pass -X GET https://www.website.com/path
When I try to make the same request in VBA, though, I get a 403 error and the response appears empty. I'm using this code:
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", "https://www.website.com/path", False
objHTTP.SetCredentials "user", "pass", 0
objHTTP.Send
I've made sure the reference to the WinHttpRequest 5.1 library was set via Tools>>References, but I'm no great shakes at VBA and don't know what else I might be doing wrong to recreate the successful curl request in VBA. C# WebRequest but not cURL Gives Error 403 seems to have a similar problem, but the answerer says curl also gives a 403, which isn't the case for me - and in any event, there doesn't seem to be a version of HttpClient for VBA, at least according to the documentation and this other question: VBA using HttpClient to connect to an external REST API.
I use an alternative in Access without any issues.
Dim WinHttp_ As Object
Set WinHttp_ = CreateObject("MSXML2.XMLHTTP")
WinHttp_.Open "GET", <url>, False, <user>, <password>
WinHttp_.Send
If WinHttp_.Status = 200 Then
'do whatever you need through WinHttp_.ResponseBody or WinHttp_.responseText
End If
Set WinHttp_ = Nothing
You may need to check the WinHttp_.ReadyState before checking the WinHttp_.Status.
https://msdn.microsoft.com/en-us/library/hh772834(v=vs.85).aspx
I was able to resolve my issue by switching to MSXML2 and encoding the authorization header in base 64. Not sure why this was necessary, but I'm leaving this here in case some other poor lost soul ends up with the same problem.
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
With objHTTP
.Open "GET", URL, False
.setRequestHeader "Authorization", "Basic " + Base64Encode("user:pass")
.setRequestHeader "Content-Type", "application/json"
.Send Body
End With

Siteminder excel vba authentication

I need to use jira Rest API to fetch some content in an excel sheet.The problem is that the server is siteminder enabled.I am using this codeproject article as reference (.http://www.codeproject.com/Articles/80314/How-to-Connect-to-a-SiteMinder-Protected-Resource ) but still cannot get to the past the siteminder login page
For reference,my code is as follows:
Dim http As New WinHttp.WinHttpRequest
http.Option(WinHttpRequestOption_EnableRedirects) = False
url = "http://example.com"
' Launch the HTTP request
http.Open "GET", url, False
http.Send
target=http.GetResponseHeaders("location")//get the target url for post
Cookie=""
postData="USER="&HttpUtility.UrlEncode(username)&"&PASSWORD="&password
http.Open "POST", target, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "Cookie", Cookie
http.Send postData
The first http request takes me to the userlogin page of siteminder but the user doesn't get authenticated after the post request.I receive the error login page.Any suggestion as to how to get the user authenticated?