The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

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

Highlighted
Occasional 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:


<code>



 


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

 


lCredntials.Add(


 


 


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 =


"application/x-www-form-urlencoded"

 


data.Append(


data.Append(


 


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

data.Append(


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

MsgBox(strResponse)


 


ResponseTextBox.Text = ex.Message


 


 


Catch ex As ExceptionEnd Try

 


End Sub

</code>


TIA


 


 



 



Private

2 REPLIES 2
Highlighted
Employee

401 Authentication

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

Highlighted
Occasional Participant

Now receiving 400 Bad Request

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:


<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))


Next


</code> 


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?


Thanks!


 


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">

      <Type>ADD_CONTACTS</Type>

      <Status>PENDING</Status>

      <TransactionCount>11631</TransactionCount>

      <Errors>0</Errors>

      <InsertTime>2010-06-13T01:23:41.732Z</InsertTime>

    </Activity>

  </content>

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

  <id>http://api.constantcontact.com/ws/customers/username/activities/xxxxxx</id>

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

  <updated>2010-06-13T01:23:41.732Z</updated>

  <author>

    <name>Constant Contact</name>

  </author>

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

</entry>


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

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured