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