0

I have a page that calls an api that in test mode has not required any authorization.
We are now moving to a live environment where a username and password will be required.

The api provider has sent the following message:

To access these services please send the requests by adding following HTTP header. Authorization: Basic Base64Encode(“username: password”)

I'm not sure of the correct syntax and wondered if someone could help me out.

The original call (and working perfectly) is:

Dim xmlobj, username, password
username="myusername"
password="mypassword"
Set xmlobj = server.CreateObject("MSXML2.DOMDocument.3.0")
xmlobj.async = false
xmlobj.setProperty "ServerHTTPRequest", True
xmlObj.AddHeader "Authorization", "Basic", Base64Encode(username & ":" & password)
xmlobj.load(sUrl)

The above code throws an error

/api-test.asp |20|800a000d|Type_mismatch:_'Base64Encode'

Any help would be greatly appreciated.

10
  • Base64Encode() is a suggested function (not built in but there are many examples online) to base64 encode the string "username:password" which is passed in the Authorization header. Once you have the function the AddHeader should look like xmlObj.AddHeader "Authorization", "Basic", Base64Encode(username & ":" & password) (assuming username and password are variables that have been set earlier. At the moment your just passing a string of "Base64Encode('username','password')" which the server will reject.
    – user692942
    Commented Jan 28, 2015 at 11:34
  • Thanks for the reply. Changed the code to: xmlObj.AddHeader "Authorization", "Basic", Base64Encode(username & ":" & password) as suggested (variables are set). I'm now getting the error - /api-test.asp |20|800a000d|Type_mismatch:_'Base64Encode' - any suggestions? Commented Jan 28, 2015 at 14:11
  • It's like I said the function Base64Encode() is not known Classic ASP, but there are plenty of examples of that function on the web. That error is telling you it doesn't understand what Base64Encode() is, to correct this copy the function definition from somewhere on the web into your ASP code.
    – user692942
    Commented Jan 28, 2015 at 15:42
  • Maybe this will help Base64 encode VBS function (vb encoder algorithm), source code. My guess is this is what the API example was taken from.
    – user692942
    Commented Jan 28, 2015 at 15:44
  • Hi, sorry for the delay in getting back to you - still trying to get my head around what you have posted. If I may try stating this in laymans terms. I need to Base64Encode the username and password in a function before passing them to the api in the xmlObj.AddHeader. I assume I can Base64Encode the user / pass using an online function and then hard code them in the call?? Commented Jan 29, 2015 at 22:40

1 Answer 1

0

Like I've mentioned in the comments, the syntax for the Authorization header is incorrect.

The API example doesn't expect "Base64Encode('username','password')" this is an example supplied by the API to show you how to take the string "username:password" and Base64 encode it which is what the Authorization header is expecting.

But you still need to have the Base64Encode() function definition for the code to work.

Base64Encode() and MyASC() functions are taken from Base64 encode VBS function (vb encoder algorithm), source code

Something like this should work;

<%
Function Base64Encode(inData)
  'rfc1521
  '2001 Antonin Foller, Motobit Software, http://Motobit.cz
  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim cOut, sOut, I

  'For each group of 3 bytes
  For I = 1 To Len(inData) Step 3
    Dim nGroup, pOut, sGroup

    'Create one long from this 3 bytes.
    nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
      &H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))

    'Oct splits the long To 8 groups with 3 bits
    nGroup = Oct(nGroup)

    'Add leading zeros
    nGroup = String(8 - Len(nGroup), "0") & nGroup

    'Convert To base64
    pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)

    'Add the part To OutPut string
    sOut = sOut + pOut

    'Add a new line For Each 76 chars In dest (76*3/4 = 57)
    'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
  Next
  Select Case Len(inData) Mod 3
    Case 1: '8 bit final
      sOut = Left(sOut, Len(sOut) - 2) + "=="
    Case 2: '16 bit final
      sOut = Left(sOut, Len(sOut) - 1) + "="
  End Select
  Base64Encode = sOut
End Function

Function MyASC(OneChar)
  If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function

Dim xmlobj, username, password
username="myusername"
password="mypassword"
Set xmlobj = server.CreateObject("MSXML2.XMLHTTP.3.0")
xmlobj.Open "GET", sUrl, False
xmlobj.setRequestHeader "Authorization", "Basic " & Base64Encode(username & ":" & password)
xmlobj.Send

Dim xmldoc
If xmlobj.status = 200 Then
  Set xmldoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
  xmldoc.Load xmlobj.ResponseXML
Else
  Response.Write "An error occurred!"
End If
%>
4
  • OK couple of little errors in the code. xml.Open need to be xmlobj.Open and "Basic", Base64Encode(username & ":" & password) needs to be "Basic"&Base64Encode(username & ":" & password) (I think!!) or the error Wrong_number_of_arguments_or_invalid_property_assignment:_'setRequestHeader' is thrown. After those changes are made I now get this error: Security_certificate_required_to_access_this_resource_is_invalid. Commented Jan 31, 2015 at 13:03
  • @user2369824 You're right sorry was just pseudo coded. Have corrected it but I think the "Basic"&Base64Encode(username & ":" & password) needs a space (see my edit). For example it should end up like Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== (example taken from Wikipedia).
    – user692942
    Commented Jan 31, 2015 at 14:34
  • Amended the code accordingly and to solve the security certificate issue in my previous comment I call my page over https. Now it takes around 10 seconds for the page to load - but I just get a blank page. Commented Feb 1, 2015 at 8:15
  • @user2369824 Of course you will have a blank page it loads the xml into an XMLDOM object but what does the code do next? You have to tell it to display. I thought you said this worked previously? What is it meant to do?
    – user692942
    Commented Feb 1, 2015 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.