VBA XMLHTTP disable authentication pop-up - vba

With reference to this question VBA XMLHTTP clear authentication? I am also trying to pass basic authentication in VBA. It works perfectly.
My only concern is login pop up which comes up whenever a wrong userid or password is sent in request. Is there a way to disable this and send a message box to user from VBA itself telling him about authentication failure?
Update
VBA code:
Dim objHTTP As New MSXML2.XMLHTTP60
With objHTTP
.Open "GET", UrlUserService, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Authorization", "Basic " + Base64Encode(userName + ":" + userPwd)
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send ""
response = .Status
End With
Popup

Use Msxml2.XMLHTTP.6.0 protocol and pass the credentials as part of request
.Open "GET", pUrl, false, "pUsername", "pPassword"

Related

Authorization data saved in xmlhttprequest with VBA

I am calling a Webservice with basic authentication with VBA. The problem I have is that the user/password is stored after the first successful call. That means I can put wrong user/password in the next calls and it works just the same.
Set objRequest = CreateObject("MSXML2.XMLHTTP")
blnAsync = True
With objRequest
.Open "GET", strUrl, blnAsync
.setRequestHeader "Authorization", "Basic " + EncodeBase64(user + ":" + passw)
.Send
'spin wheels whilst waiting for response
While objRequest.readyState <> 4
DoEvents
Wend
strResponse = .responseText
End With
If I call the Webservice from browser it is the same. First time I need to login, next time it works without login. After I delete cache/cookies/history I need to login again.
My questions:
Where is this (cache?) data stored if I call from VBA and how to delete this?
How to prevent VBA from saving the authorization data?
I found the crucial hint to put user/password in the .Open-method here: VBA XMLHTTP disable authentication pop-up
That's how it works for me. The only thing I can't do now is encoding the user & password.
Dim objRequest As MSXML2.XMLHTTP30
Set objRequest = New MSXML2.XMLHTTP30
With objRequest
.Open "GET", strUrl, blnAsync, user, passw
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
While objRequest.readyState <> 4
DoEvents
Wend
strResponse = .responseText
End With
To stop the caching, you could try adding:
.setRequestHeader("Cache-Control", "no-cache");
.setRequestHeader("Pragma", "no-cache");
.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
See also VBA XMLHTTP clear authentication? for more information.

VBA RESTful API GET Method issue

Hi I'm calling an API and using the winhttp request and the GET method. I'm passing a json style parameter in the send method but it just can't get accepted. I end up with the error message:
{"code":"INS03","description":"Event ID required","requestId":"_181603230829162847306080","data":{},"validationErrors":null}
Which seems weird because I am indeed passing the event id parameter, as follows:
inventory_URL = "https://api.stubhub.com/search/inventory/v1"
Dim oRequest As WinHttp.WinHttpRequest
Dim sResult As String
Set oRequest = New WinHttp.WinHttpRequest
With oRequest
.Open "GET", inventory_URL, True
.setRequestHeader "Authorization", "Bearer " & access_token
.setRequestHeader "Accept", "application/json"
.setRequestHeader "Accept-Encoding", "application/json"
.send ("{""eventid"":""9445148""}")
.waitForResponse
sResult = .responseText
Debug.Print sResult
sResult = oRequest.Status
Debug.Print sResult
End With
Is there any issue with my code?
Thank you in advance,
Vadim
For GET request query string should be composed.
Your data can't be passed in Send but in query string:
.Open "GET", inventory_URL & "?eventid=9445148", True
Check GET vs POST.

Non-interactive api login after windows 10 update

I was using this method to login to betfair api. Everything works fine, but after Windows 10 update (KB3140741) not working anymore. ResponseText = {"loginStatus":"CERT_AUTH_REQUIRED"} Anyone solved this?
Windows 10 Build 10586.218, version 1511
Microsoft Office 2016
Dim oHTTP As Object: Set oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim uri As String: uri = "https://identitysso.betfair.com/api/certlogin"
oHTTP.Open "POST", uri, False
oHTTP.SetClientCertificate "Common Name"
oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oHTTP.setRequestHeader "X-Application", App_key
oHTTP.setRequestHeader "Accept", "application/json"
oHTTP.send "username=" & UserName & "&password=" & Password & ""
I would try with Msxml2.ServerXMLHTTP.6.0 instead :
Const uri = "https://identitysso.betfair.com/api/certlogin"
Dim req As Object
Set req = CreateObject("Msxml2.ServerXMLHTTP.6.0")
req.Open "POST", uri, False
req.setOption 2, 13056 ' ignore all certificate errors '
req.setOption 3, "Common Name" ' set the client certificate from the local store '
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.setRequestHeader "X-Application", App_key
req.setRequestHeader "Accept", "application/json"
req.send "username=" & UserName & "&password=" & Password & ""

Why won't this VBA program using ServerXMLHTTP60 authenticate properly?

I have 4 different queries of IPR 1.2.3.4 (ip-reputation) to IBM's Xforce database, using basic authentication (base64 encoded), as well as URL-reputation. My version in python works great, printing out the appropriate JSON information:
...
if __name__ == '__main__':
apiKey = "1234...."
apiPwd = "5678..."
result = requests.get('https://xforce-api.mybluemix.net:443/ipr/1.2.3.4', verify=False,auth=(apiKey, apiPwd))
if result.status_code != 200:
print( "~ Bad Status Code: {}".format(result.status_code))
else:
print("~ The result is {}".format(result.status_code))
print("~ Rx Data={}".format(result._content))
The version in Go (using demisto's goxforce from github) works great. After setting my environment-variable key and password, I issue the commandline:
'xforceQuery -cmd ipr -q 1.2.3.4'
and it prints out the json information about 1.2.3.4, again, perfectly.
I use the browser-utility called 'Postman', specify basic authentication with my user/key and password, headers of Accept: application/json, Accept-Language: en, and Content-Type: application/json, and, again, it gives me the proper information (see .gif, below)
On the other hand, I try the same thing in VBA, and I get '401 Error: Not authorized'. What's wrong with this code?
Public Sub testXF()
Dim myKey As String
Dim myPass As String
myKey = "1234..."
myPass = "5678..."
pHtml = "xforce"
Dim ohttp As MSXML2.ServerXMLHTTP60
Set ohttp = New MSXML2.ServerXMLHTTP60
If timeout = 0 Then timeout = 60
ohttp.Open "GET", "https://xforce-api.mybluemix.net:443/ipr/1.2.3.4", False, myKey, myPass
With ohttp
.SetRequestHeader "Content-Type", "application/json"
.SetRequestHeader "Accept", "application/json"
.SetRequestHeader "Accept-Language", "en"
.SetTimeouts 0, 30 * 1000, 30 * 1000, timeout * 1000
.SetRequestHeader "Authorization", "Basic " + _
Base64Encode(myKey + ":" + myPass)
.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
End With
ohttp.Send ("")
With ohttp
pStatus = .status
pText = .ResponseText
pResponseHeaders = .GetAllResponseHeaders()
End With
Debug.Print "GET", pStatus, pText
Set ohttp = Nothing
End Sub

Access Denied when checking file exists on sharepoint from vba

I am trying to check if a file exists in SharePoint 2010 from Excel 2010 VBA. I took this code from another question.
Function checkFile(URLStr As String) As Boolean
Dim oHttpRequest As Object
Set oHttpRequest = New MSXML2.XMLHTTP60
With oHttpRequest
.Open "GET", URLStr, False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
End With
If oHttpRequest.Status = 200 Then
checkFile = True
Else
checkFile = False
End If
End Function
When I do so, vba is throwing an error: 'Access Denied Error for this object'
It fails on the line .send
I figured out that the error code is -2147024891.
I checked the Sharepoint permissions and they should be fine.
My URL with http, but access to sharepoint is granted with https only (at lest at a certain version). Changing the URL solved the access issues.
Function checkFile( URLStr As String) As Boolean
Dim oHttpRequest As Object
Dim GetResult As Integer
Set oHttpRequest = New MSXML2.XMLHTTP60
With oHttpRequest
.Open "GET", URLStr, False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
End With
On Error GoTo HttpError
oHttpRequest.send '!!!!!!here code stops!!!!!!!
HttpError:
GetResult = 00
GetResult = oHttpRequest.Status
If GetResult = 200 Then
checkFile = True
Else
checkFile = False
End If
End Function
You need to define the authentication user info.
oHttp.SetRequestHeader "Authorization", "Basic " + _
Base64Encode(authUser + ":" + authPass)
Here is some info
http://ramblings.mcpher.com/Home/excelquirks/snippets/basicauth
How to pass authentication credentials in VBA