A bit confused....

SOLVED
Highlighted
Occasional Advisor

A bit confused....

Sorry if this is a silly question, I'm trying to troubleshoot my Bad Request (400) error.

 

I'm using the code example provided here to use the CC API:

http://www.thisisvisceral.com/2008/09/using-the-constant-contact-api/

 

First off, is this still even valid code for CC? I noticed it's somewhat old, so I'm not sure if it's still compatible with CC.

 

Second, I notice a lot of people posting raw XML when asking for help. I've also seen StringBuilder outputs posted here, very similar to my output(that still gets a 400 error):

 

activityType=SV_ADD&data=Email+Address%2cFirst+Name%2cLast+Name%0aTEST%40CROWNPOINTSOLUTIONS.COM%2cJR%2cTEST&lists=https%3a%2f%2fapi.constantcontact.com%2fws%2fcustomers%2fnationalwesternlists%2f2

 

Can someone explain the difference between these two approaches? Is this the difference between Basic Authentication and OAuth?  Apologies if this post is answered nearby, I'm new to CC and it's community. Thank you for any help! :)

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Advisor

Re: A bit confused....

After a bit of due dilligence, I realized that a (409) "error" was a post-process response. The contact was added to the proper list, and everything worked fine except for the 409 response. Since I'm still pretty new to this API, I am hoping someone can answer why I'm getting that 409.

 

My guess is that I'm trying to create a NEW list with the ID of 2? Other than that I don't know why I'd get a 409 conflict. The new contact's info is unique. Any ideas? 

 

The code:

 

string CCusername = "nationalwestern";
        string CCpassword = "PASSWORD REMOVED";
        string CCuri = "https://api.constantcontact.com/ws/customers/" + CCusername + "/activities";
        string CClistUri = "https://api.constantcontact.com/ws/customers/" + CCusername + "lists/2";
        string CCAPIKEY = "API KEY HERE";

        AuthenticationData authdata = new AuthenticationData();

        authdata.ApiKey = CCAPIKEY;
        authdata.Username = CCusername;
        authdata.Password = CCpassword;
        
        Contact theContact = new Contact();

        theContact.EmailAddress = email;

        ContactOptInList theList = new ContactOptInList();
        theContact.OptInSource = ContactOptSource.ActionByContact;
        theList.ContactList = new ContactList("2");
        theContact.ContactLists.Add(theList);
        
        ConstantContactUtility.Utility.CreateNewContact(au​thdata, theContact);

  Also, currently I'm "catching" that exception on my end and not doing anything with it, just carrying on with my internal operations and a redirect. If I can't resolve that 409, does anyone see anything terrible with leaving it the way it is?

 

Thanks for any tips!

 

View solution in original post

7 REPLIES 7
Highlighted
Moderator

Re: A bit confused....

Hey,

 

From the example you provided, it sounds like you're using Visual Basic. Is that correct? I'd like confirmation on that before moving on, to provide you with the best answers we can.

Nick Galbraith
Support Engineer
Highlighted
Occasional Advisor

Re: A bit confused....

Hi Nick,

 

Actually I rewrote it in C#, which could be a source of the problem. I was unable to find the C# equivalent of casting a WebRequest to an HttpWebRequest:

 

The VB version was this:

Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)

 

My C# version is this:

WebRequest request = WebRequest.Create(address);

 

Effectively I'm using a WebRequest object instead of a HttpWebRequest object. I wouldn't be surprised if that causes an issue.

 

Other than that, the rest of the code ported over to C# without a problem. Here's the whole thing, and thank you for any help:

 

 string CCusername = "nationalwestern";
        string CCpassword = "PASSWORD REMOVED";
        string CCuri = "https://api.constantcontact.com/ws/customers/" + CCusername + "/activities";
        string CClistUri = "https://api.constantcontact.com/ws/customers/" + CCusername + "lists/2";
        string CCAPIKEY = "API KEY HERE";

        Uri address = new Uri(CCuri);
        WebRequest request = WebRequest.Create(address);
        request.Credentials = new NetworkCredential(CCAPIKEY + "%" + CCusername, CCpassword);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        StringBuilder data = new StringBuilder();
        data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8));
        data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,First Name,Last Name" + "\n"), Encoding.UTF8));
        data.Append(HttpUtility.UrlEncode((email + "," + first_name + "," + last_name), Encoding.UTF8));
        data.Append("&lists=" + HttpUtility.UrlEncode(CClistUri));

        Byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());        
        String st = String.Empty;
        request.ContentLength = byteData.Length;

        Stream postStream = request.GetRequestStream();
        using (postStream)
        {
            postStream.Write(byteData, 0, byteData.Length);
        }       

        WebResponse response = request.GetResponse();      

        using (response)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            st = reader.ReadToEnd();
        }      

 

FYI, I intentionally removed the account password and the API key data from the code.

 

Highlighted
Regular Advisor

Re: A bit confused....

Hi Bonnie,

 

The correct syntax for casting the request in C# is the following:

 

HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;

Also, we do have a C# wrapper and sample forms listed on the following page: http://community.constantcontact.com/t5/Documentation/Code-Samples/ba-p/25019. Using that should save you some time.

Richard Maxwell
Support Engineer, Constant Contact
Highlighted
Occasional Advisor

Re: A bit confused....

Thanks for the reply and the casting help.

 

What I am trying to do is very simple. We have a simple eNewsletter sign up form in .NET (C#) on our site. Email address, First Name and Last Name, that's it. I've put the ConstantContactBO.dll and the ConstantContactUtility.dll files in the 'bin' folder of my solution. These libraries are available within my solution, everything builds fine. I've integrated functionality like this in the past without problems, is there something I'm missing?

 

I have been unable to find just plain old C# code that would fire on a Click event to add the new user when the user submits form data. Does that exist in the Wrapper V0.1 solution or am I looking in the wrong place? I've only found 2 C# examples total, the one above and another that returned (409) errors. I've tried wrapping the request in a Try/Catch and spitting out the exception message, but that doesn't lend any useful info.

 

Thank you for any C# code-behind examples you can point me to, or any other advice that would help achieve this simple task.

 

EDIT: Here's the other C# example I found that returned 409 errors:

string CCusername = "nationalwestern";
        string CCpassword = "PASSWORD REMOVED";
        string CCuri = "https://api.constantcontact.com/ws/customers/" + CCusername + "/activities";
        string CClistUri = "https://api.constantcontact.com/ws/customers/" + CCusername + "lists/2";
        string CCAPIKEY = "API KEY HERE";

        AuthenticationData authdata = new AuthenticationData();

        authdata.ApiKey = CCAPIKEY;
        authdata.Username = CCusername;
        authdata.Password = CCpassword;
        
        Contact theContact = new Contact();

        theContact.EmailAddress = email;

        ContactOptInList theList = new ContactOptInList();
        theContact.OptInSource = ContactOptSource.ActionByContact;
        theList.ContactList = new ContactList("2");
        theContact.ContactLists.Add(theList);
        
        ConstantContactUtility.Utility.CreateNewContact(authdata, theContact);

 

Highlighted
Occasional Advisor

Re: A bit confused....

After a bit of due dilligence, I realized that a (409) "error" was a post-process response. The contact was added to the proper list, and everything worked fine except for the 409 response. Since I'm still pretty new to this API, I am hoping someone can answer why I'm getting that 409.

 

My guess is that I'm trying to create a NEW list with the ID of 2? Other than that I don't know why I'd get a 409 conflict. The new contact's info is unique. Any ideas? 

 

The code:

 

string CCusername = "nationalwestern";
        string CCpassword = "PASSWORD REMOVED";
        string CCuri = "https://api.constantcontact.com/ws/customers/" + CCusername + "/activities";
        string CClistUri = "https://api.constantcontact.com/ws/customers/" + CCusername + "lists/2";
        string CCAPIKEY = "API KEY HERE";

        AuthenticationData authdata = new AuthenticationData();

        authdata.ApiKey = CCAPIKEY;
        authdata.Username = CCusername;
        authdata.Password = CCpassword;
        
        Contact theContact = new Contact();

        theContact.EmailAddress = email;

        ContactOptInList theList = new ContactOptInList();
        theContact.OptInSource = ContactOptSource.ActionByContact;
        theList.ContactList = new ContactList("2");
        theContact.ContactLists.Add(theList);
        
        ConstantContactUtility.Utility.CreateNewContact(au​thdata, theContact);

  Also, currently I'm "catching" that exception on my end and not doing anything with it, just carrying on with my internal operations and a redirect. If I can't resolve that 409, does anyone see anything terrible with leaving it the way it is?

 

Thanks for any tips!

 

View solution in original post

Highlighted
Occasional Advisor

Re: A bit confused....

*sigh*

 

You know, it's usually best to check all your code before posting on a forum and looking like a fool. LOL.

 

I was getting the 409 due to some other code in the page firing the Click event a second time. So the 409 was simply your awesome API telling me that the contact had already been submitted. D'oh!

 

Case closed. Carry on! :)

Highlighted
Moderator

Re: A bit confused....

Glad everything got figured out! Give us a holler if you need some support, and we'll be around.
Nick Galbraith
Support Engineer
Developer Portal

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

Visit Page

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured