We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

API Key development 401 Unauthorized error on my web page

Regular Participant

API Key development 401 Unauthorized error on my web page

I'm receiving a 401 unauthorized error on my web site page. This is being displayed before I fill in the form to be submitted.


In order to give you an answer or lead on this, we will need to see your code for you web form and your script.  It does sound like you are posting content or calling a script function prior to filling out the form though.

Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

I'm trying to get my connection to the server so my form is not filled out.


FirstName="First Name" //Your Contact's First Name

LastName="Last Name" //Your Contact's Last Name

EmailAddress="Email Address" //Your Contact's Email Address

PhoneNumber="Phone Number" //Your Contact's Phone Number

PostalCode="Zip1" //Your Contact's Zip Code

UN = "XXXXXX" //Your Account Username

PW = "XXXXXX" //Your Account Password

OptInSource="ACTION_BY_CUSTOMER" //The Actoin Type, usually as listed

API_Key = "XXXXXXXX" //Your API Key

List_ID="1,2,3,4,5,6" //Your Particular List ID,,1,2,3 & etc. (Default is 1, first user created would be 2, and so on)

response.write(PostDataToURL(List_ID,FirstName,LastName,EmailAddress,PhoneNumber,PostalCode, "POST", UN,PW,API_Key))

Function PostDataToURL(List_ID,FirstName,LastName,EmailAddress,PhoneNumber,PostalCode, strMethod, UN,PW,API_Key)

Dim lngTimeout

Dim strUserAgentString

Dim intSslErrorIgnoreFlags

Dim blnEnableRedirects

Dim blnEnableHttpsToHttpRedirects

Dim strHostOverride

Dim strLogin

Dim strPassword

Dim strResponseText

Dim objWinHttp

Dim entry

Dim Base_URL

Dim UpdateTimeStamp


lngTimeout = 59000

strUserAgentString = "http_requester/0.1"

intSslErrorIgnoreFlags = 0 ' 13056: ignore all err, 0: accept no err

blnEnableRedirects = True

blnEnableHttpsToHttpRedirects = True

strHostOverride = ""

strLogin = API_Key & "%" & UN

strPassword = PW

UpdateTimeStamp = date()

strPostData="<entry xmlns=""http://www.w3.org/2005/Atom"">"&vbcrlf&_

"<title type=""text""> </title>"&vbcrlf&_




"<summary type=""text"">Contact</summary>"&vbcrlf&_

"<content type=""application/vnd.ctct+xml"">"&vbcrlf&_

"<Contact xmlns=""http://ws.constantcontact.com/ns/1.0/"">"&vbcrlf&_








"<ContactList id="""&Base_URL&"lists/"&List_ID&""" />"&vbcrlf&_





Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")

objWinHttp.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout

objWinHttp.Open strMethod,Base_URL&"contacts"

If strMethod = "POST" Then

objWinHttp.setRequestHeader "Content-type", _


End If

If strHostOverride <> "" Then

objWinHttp.SetRequestHeader "Host", strHostOverride

End If

objWinHttp.Option(0) = strUserAgentString

objWinHttp.Option(4) = intSslErrorIgnoreFlags

objWinHttp.Option(6) = blnEnableRedirects

objWinHttp.Option(12) = blnEnableHttpsToHttpRedirects

If (strLogin <> "") And (strPassword <> "") Then

objWinHttp.SetCredentials strLogin, strPassword, 0

End If

On Error Resume Next


If Err.Number = 0 Then

If objWinHttp.Status = "200" Then

PostDataToURL = objWinHttp.ResponseText


select case objWinHttp.Status

case "409"

PostDataToURL="2"&vbtab&":Contact Already Exists"

case "201"

PostDataToURL="1"&vbtab&":Contact Successfully added"

case "400"

PostDataToURL="0"&vbtab&":Bad Request. Check Submitted Data: "

case else

PostDataToURL = "0"&vbtab&":" & objWinHttp.Status & " " &objWinHttp.StatusText

end select

End If


PostDataToURL = "Error " & Err.Number & " " & Err.Source & " " & _


End If

On Error GoTo 0

Set objWinHttp = Nothing

End Function



It looks like you're not posting to the correct URI.  Using Basic Authentication (which is the default for SetCredentials in WinHTTP), you will need to post to the https URI.




That should allow you to get by the 401 response.

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

View API documentation, code samples, get your API key.

Visit Page