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.

Recieving 401 (Authorization Error) from VB.NET API (add contacts)

Regular Participant

Recieving 401 (Authorization Error) from VB.NET API (add contacts)

I have looked at previous posts regarding the 401 error-- and solutions -- without any success.

What I have done is to create a VB.Net application that gets data in an XML format, creates an Excel file, and attempts to load it into a test list. Eveything goes great until I run the API, and I then receive a 401 error, even after I checked the API information (API Key, user name, password) sent.

I found one suggestion to use the URL https://api.constantcontact.com/ws/customers/<username>/contact that causes a prompt for the user ID and password to appear, but nothing that I have will authenticate. (using my actual user name where <username> is above, of course.

I can post my code here, but it is pretty much copied from the sample, and I am thinking that unless I can get the above link to work & authenticate, that it won't matter.

I would appreciate any direction you can provide.

Here is the code, if it helps, with the security stuff x'ed out:



Sub uCC(ByVal aData)    Try Dim cUser As String = String.Empty & "username"


Dim cPass As String = String.Empty & "password"


Dim cUrl As String = "https://api.constantcontact.com/ws/customers/" & cUser & "/activities"


' List 5 is test, 1 is production


Dim cList As String = "http://api.constantcontact.com/ws/customers/" & cUser & "/lists/5"


Dim cApi As String = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"


'setup httpWebReqeust to send the data





theRequest.Credentials = lCredntials

theRequest.Method =

Dim lCredntials = New CredentialCache()New Uri("https://api.constantcontact.com/ws/customers/" & cUser), "Basic", New NetworkCredential((cApi & "%" & cUser), cPass))Dim address As New Uri(cUrl)Dim theRequest As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)"POST"

theRequest.ContentType =






data.Append(HttpUtility.UrlEncode(Chr(10), Encoding.UTF8))

data.Append(HttpUtility.UrlEncode((l.ToString), Encoding.UTF8))


Dim data As New StringBuilder()"activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8))"&data=" + HttpUtility.UrlEncode(("EmailAddress"), Encoding.UTF8))For Each l In aDataNext


EncodedURITextBox.Text = data.ToString()



theRequest.ContentLength = byteData.Length


postStream.Write(byteData, 0, byteData.Length)


"&lists=" + HttpUtility.UrlEncode(cList))Dim byteData As Byte() = UTF8Encoding.UTF8.GetBytes(data.ToString())Dim strResponse As String = String.EmptyUsing postStream As Stream = theRequest.GetRequestStream()End Using



strResponse = reader.ReadToEnd()

ResponseTextBox.Text = strResponse


Using theResponse As HttpWebResponse = TryCast(theRequest.GetResponse(), HttpWebResponse)Dim reader As New StreamReader(theResponse.GetResponseStream())End Using



ResponseTextBox.Text = ex.Message



Catch ex As ExceptionEnd Try


End Sub








If you are visiting https://api.constantcontact.com/ws/customers/<username>/contact, the prompt that comes up will be asking for your login credentials. The username for this would be in the format of apikey%username, and your password would be your typical Constant Contact password. If you are unable to access this resource with your credentials, I would recommend verifying that your login credentials are correct.


Please note that the username and password are for your Constant Contact account, and not your developer board account name. To verifying if you are using the correct username and password, you should be able to login to www.constantcontact.com using these credentials. If you are unable to login, then either your username or password are incorrect or have been modified.


If you are still unable receiving an error 401, please let me know and we can continue to troubleshoot further.

David J

Regular Participant

I was not using the correct user name in my code (using the developer user, not the normal user). Now I am receiving the 400 Bad Request from the application. I will keep digging as I know other folks have reported this...and I will update here if I find a solution. In the meantime, I would appreciate any assistance you can offer.

 After checking on the other 400 errors reported, I am looking at my excel file. All I have is an email Address in column A.

I am looking at this code:


data.Append("&data=" + HttpUtility.UrlEncode(("Email Address"), Encoding.UTF8))

For Each l In aData 


data.Append(HttpUtility.UrlEncode(vbLf, Encoding.UTF8))

data.Append(HttpUtility.UrlEncode((l.ToString), Encoding.UTF8))



I can look at the excel file fine and there appears to be no problems there.

Is there a way to directly import the XML?



OK, I ditched the Excel file, and created a txt file and got the following result:

<?xml version="1.0" encoding="UTF-8"?>

<entry xmlns="http://www.w3.org/2005/Atom">

  <content type="application/xml">

    <Activity xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/username/activities/xxxxxx">








  <link href="/ws/customers/username/activities/xxxxx" rel="edit"></link>


  <title type="text">ADD_CONTACTS: Unnamed</title>



    <name>Constant Contact</name>


  <link href="/ws/customers/username/activities/xxxxx" rel="edit"></link>


I think it is good...will wait until later to check if the "PENDING" gets added to my test list!

Developer Portal

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

Visit Page