Saturday, 22 August 2009

Basic Authentication of SOAP from Excel VBA

I recently coded up Excel in VBA as a Web Service data fetching using SOAP, Apache AXIS and a MatrixOne instance.

Matrix only supports plain authentication over Axis so I had little choice but to use it, if you have support for SOAP's authentication mechanism, i suggest you use it. Unfortunately it took me hours of googling + recruiting a colleague to filter out all the crap tutorials to figure out how, so here is the code.

Private Const c_WSDL_URL As String = "https://address/path/to/service?wsdl"
Private Const c_SERVICE As String = "ServiceName"
Private Const c_PORT As String = "PortName"
Private Const c_SERVICE_NAMESPACE As String = "http://my.name.space.com"

' Create the client
Set client = New SoapClient30    
' Initialize the client from the WSDL, populating available functions
client.MSSoapInit2 c_WSDL_URL, str_WSML, c_SERVICE, c_PORT, c_SERVICE_NAMESPACE
' Set the header to include login details
client.ConnectorProperty("RequestHTTPHeader") = "Authorization: Basic " & Base64Encode("username:password")