Must supply a valid HTTP Basic Authorization header - Classic ASP

This is my code so far, i'm new.

'-------
Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument")
'-----
XMLHTTP.Open "POST", "http://api.createsend.com/api/v3/subscribers/{listid}.{json}", False, "", ""
xmlHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "Authorization", "Basic {APIKey}:{dummypassword}")
xmlHTTP.send "<Client><CompanyName>MyCompany</CompanyName><ContactName>John</ContactName><EmailAddress>john@whatsit.com</EmailAddress><Country>Australia</Country><TimeZone>(GMT+10:00) Canberra, Melbourne, Sydney</TimeZone></Client>"

response.write xmlhttp.responseText

Result
Must supply a valid HTTP Basic Authorization header

Obviously i don't really know what i'm doing.

I read the adding subscriber page here http://www.campaignmonitor.com/api/subscribers/

And it says to post to

http://api.createsend.com/api/v3/subscribers/{listid}.{xml|json}

So do i change that to (2234KJ is my list id for example purposes)

http://api.createsend.com/api/v3/subscribers/2234KJ.xml

And what should be in the basic authorization code?

For example my api key is GH12345

XMLHTTP.setRequestHeader "Authorization", "Basic GH12345:testpass"

?

jamesd jamesd, 5 years ago

Yes, you should be posting to http://api.createsend.com/api/v3/subscribers/2234KJ.xml if your list id is 2234KJ.

Please see this example of how you do basic HTTP authentication.

You will need to make sure you base64 encode the content of the Authorization header, so the header will end up looking something like:

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

rather than:

Authorization: Basic "username:password"

The content of the body of the request in your first code sample looks incorrect for the route you are using. You probably want something more similar to:

<Subscriber>
  <EmailAddress>subscriber@example.com</EmailAddress>
  <Name>New Subscriber</Name>
  <CustomFields></CustomFields>
  <Resubscribe>true</Resubscribe>
</Subscriber>
mrfusion, 5 years ago

Thanks! I think i'm getting on track but have a new error.

Failed to deserialize your request. Please check the documentation and try again. Fields in error: subscriber

This is my code now

Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument")

Dim apikey
Dim pass
''api key is 0000G as an example
apikey="0000G"
pass="dummypassword"

XMLHTTP.Open "POST", "http://api.createsend.com/api/v3/subscribers/2234KJ.xml", false
xmlHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "Authorization", "Basic " & Base64Encode(apikey & ":" & pass) 
xmlHTTP.send "<Subscriber><EmailAddress>1234@gmail.com</EmailAddress><Name>FrederickVonGoobenstein</Name><CustomFields><FirstName></FirstName><LastName></LastName><State></State><Suburb></Suburb></CustomFields><Resubscribe>true</Resubscribe></Subscriber>"

response.write xmlhttp.responseText

Gives the deserialize error.

mrfusion, 5 years ago

Ok don't worry, that code above did work. Woo :)

I had a typo on the <Resubscribe> tag.

So there you go future classic asp integrators. (you will have to grab a custom base64 function, many are on the web i used the below one)

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
SteVader SteVader, 4 years ago

THANK YOU mrfusion! That snippet just saved me a whole heap of pain.

winstongel, 1 year ago

Check here.....Difference between Authorization and Authentication

Winston

Join 200,000 companies around the world that use Campaign Monitor to run email marketing campaigns that deliver results for their business.

Get started for free
1-888-533-8098