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.

401 Error When Trying To Add New Contact

Highlighted
New Member

401 Error When Trying To Add New Contact

I am getting a 401 error when executing the putNewContact() Function, I have another function called getContactList() function works for me, so I don't believe my problem is in the LoginCredentials. I set the XML up to try to add a contact for testing purposes, but have not been able to get past the Response call.


Any help would be appreciated - Thanks


Private Function putNewContact() As Boolean

        Try

            Dim LoginCredentials As New CredentialCache

            LoginCredentials.Add(New Uri("https://api.constantcontact.com/ws/customers/tristatehomeservices"), "Basic", _

             New NetworkCredential("xxxxx-xxxx-xxxxx-xxxx-xxxxxxxx%{username}", "{Password}"))

            Dim Request As HttpWebRequest = WebRequest.Create("http://api.constantcontact.com/ws/customers/tristatehomeservices/contacts")

            Request.Credentials = LoginCredentials

            Request.Method = "POST"

            Request.ContentType = "application/atom+xml"


            Dim XMLData As String = getXMLData()

            Dim byteArray As Byte()

            byteArray = System.Text.Encoding.UTF8.GetBytes(XMLData)

            Request.ContentLength = byteArray.Length

            Dim streamRequest = Request.GetRequestStream()

            streamRequest.Write(byteArray, 0, byteArray.Length)



            Dim Response As WebResponse = Request.GetResponse()

            Dim Reader = New StreamReader(Response.GetResponseStream())

            Dim XMLResponse As String = Reader.ReadToEnd()

            streamRequest.Close()

            Reader.Close()

            Response.Close()



        Catch ex As Exception

            MsgBox(ex.Message)

        End Try



    End Function


    Private Function getXMLData() As String

        Try

            Dim strXMLData As New System.Text.StringBuilder

            strXMLData.Append("<entry xmlns='http://www.w3.org/2005/Atom'>")

            strXMLData.Append("<id>http://api.constantcontact.com/ws/customers/tristatehomeservices/lists/1</id>")

            strXMLData.Append("<title type='text'> </title>")

            strXMLData.Append("<updated>2008-07-23T14:21:06.407Z</updated>")

            strXMLData.Append("<author></author>")

            strXMLData.Append("<id>data:,none</id>")

            strXMLData.Append("<summary type='text'>Contact</summary>")

            strXMLData.Append("<content type='application/vnd.ctct+xml'>")

            strXMLData.Append("<Contact xmlns='http://ws.constantcontact.com/ns/1.0/'>")

            strXMLData.Append("<EmailAddress>test101@example.com</EmailAddress>")

            strXMLData.Append("<FirstName>First</FirstName>")

            strXMLData.Append("<LastName>Last</LastName>")

            strXMLData.Append("<OptInSource>ACTION_BY_CONTACT</OptInSource>")

            strXMLData.Append("<ContactLists>")

            strXMLData.Append("<ContactList id='http://api.constantcontact.com/ws/customers/tristatehomeservices/lists/Default/'>")

            strXMLData.Append("</ContactLists>")

            strXMLData.Append("</Contact>")

            strXMLData.Append("</content")

            strXMLData.Append("</entry>")

            Return strXMLData.ToString

            Exit Function

        Catch ex As Exception

            MsgBox(ex.Message)

            Return ""

        End Try

    End Function

 

2 REPLIES 2
Highlighted
Anonymous
Not applicable

You have two <id> elements in

You have two <id> elements in your XML.  Try removing the first one: <id>http://api.constantcontact.com/ws/customers/tristatehomeservices/lists/1</id>

Highlighted
Moderator

The 401 error is caused by

The 401 error is caused by your Request URI:


 


Dim Request As HttpWebRequest = WebRequest.Create("http://api.constantcontact.com/ws/customers/tristatehomeservices/contacts")


 


That should be:


 


Dim Request As HttpWebRequest = WebRequest.Create("https://api.constantcontact.com/ws/customers/tristatehomeservices/contacts")


 


Change that and you should be able to get a valid response of 201, 400, 409 or 415 depending on your actual XML content.

Dave Berard
Senior Product Manager, Constant Contact
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