How do I get all contacts in the "do not mail" list (Using API)?

Regular Participant

How do I get all contacts in the "do not mail" list (Using API)?

I could query regular lists, but how do I get all contacts in the "do not mail" system list?


(Using API of course)


 


Thanks,


Uri.


 

8 REPLIES 8
Employee

There are two ways for you to obtain which contacts are members of the ‘Do Not Mail’ list. Your specific needs will determine which method is going to work best for you.




1) You can retrieve a collection of contacts from within the ‘Do Not Mail’ list by performing an http get on the following URI:




https://api.constantcontact.com/ws/customers/{user-name}/lists/do-not-mail/members

 

This will return every subscriber who is part of the ‘Do Not Mail’ list regardless of when they when they were actually opted-out. You can find some more specifics on this at Retrieving a Contacts Collection from a Specific List.




2) You can also use Searching for Contacts by Last Updated Date (Synchronizing Contacts) to retrieve all of the contacts that have been placed on the do-not-mail list since a certain date. For example, performing an http get against the following address would return all of the contacts who have been added to the ‘Do Not Mail’ list since January 1, 2010.




https://api.constantcontact.com/ws/customers/{user-name}/contacts?updatedsince=01-01-2010&listtype=do-not-mail


 


I hope this information helps. Please let us know if you have any other questions. Thanks.

David J

Regular Participant

i can successfully send the get request from my browser: i type <API_KEY%UserName> as the user, and my password.


but programatically it would return 401 UnAuthorized.


suprisingly i CAN use the above credentials for my POST requests, and they work fine.


What am i missing? below is my .NET C# code for the GET.


(I send this string to my GET method: http://api.constantcontact.com/ws/customers/<username>/lists/do-not-mail/members.


APIKey, userName and password are static variables of my class)


Thanks!


 


 


 


private static string GET(string URI)


        {


            // Create LoginCreditials


            CredentialCache LoginCredentials = new CredentialCache();


             


            // Add a new credential for this account


            LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/"+ userName),


                // Set up URI for API site


                "Basic", // Basic login type


                new NetworkCredential(APIKey + "%" + optitexUserName, // Set up API Username (APIKey%Username)


                    password)); // Password 


                         


            // Create WebRequest, this is a factory class with a static constructor


            // You can not use new, all requests must be created with WebRequest.Create


            


            WebRequest Request = WebRequest.Create(URI); // URI for the GET 


             


            // Set Request credentials


            Request.Credentials = LoginCredentials;


             


            // Send GET request


            // Recast the response to HttpWebResponse for correct processing


            // Place in a try block to ensure that any errors are caught


            try


            {


                HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();


             


                // Process the Response as needed


                // This is a generic StreamReader to read the entire response in


                // GetResponseStream can be recast to any of the Stream types


                // Note: Remember to add using System.IO


                StreamReader Reader = new StreamReader(Response.GetResponseStream()); // Read the entire XML response to a string string


                string XMLResponse = Response.StatusCode + " " + Response.StatusDescription +" " + Reader.ReadToEnd();


             


                // Close Reader


                Reader.Close();


             


                // Close the response to free up the system resources


                Response.Close();


             


                return XMLResponse;


            }


             


            catch (WebException e)


            {


                // Get the web exception type, error number returned here


                return "WebException:" + e.Status + " " + e.Message;


            }


             


            catch (Exception e)


            {


                // Get the exception type


                return "Exception:" + e.Message;


            }


        }


 


 

Employee

I am noticing that you are using two different variables for your login information:


 


LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/"+ userName),

                // Set up URI for API site

                "Basic", // Basic login type

                new NetworkCredential(APIKey + "%" + optitexUserName, // Set up API Username (APIKey%Username)

                    password)); // Password 


 


When I used your code and modified "optitexUserName" to "userName" which is storing my actual Costant Contact username, this seemed to resolve my issue and I was successfully able to perform an HTTP get using your code. I hope this helps. If this doesn't resolve your issue please let us know.

David J

Regular Participant

 David, I'm sorry, this is not the issue, i manually changed the variable name when copied here, but forgot to change it in the second occurrence...


So i still get the 401 error... 


 

Employee

I am unable to replicate the authentication error that you are recieving. I have tested the code below and this is correctly performing an http get for me. Try changing the APIKey, userName and password from below to your login information and see if this is working for you. If you are still running into difficulties with this, please send an email to our webservices support team with your exact code and I'd be happy to take a closer look at it. Thanks.


 


// Create LoginCreditials


            String APIKey = "YOUR API KEY", userName = "YOUR USER NAME", password = "YOUR PASSWORD";

            String URI = "https://api.constantcontact.com/ws/customers/" + userName + "/contacts";


            CredentialCache LoginCredentials = new CredentialCache();


            // Add a new credential for this account

            LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + userName),

                // Set up URI for API site

                "Basic", // Basic login type

                new NetworkCredential(APIKey + "%" + userName, // Set up API Username (APIKey%Username)

                    password)); // Password


            // Create WebRequest, this is a factory class with a static constructor

            // You can not use new, all requests must be created with WebRequest.Create


            WebRequest Request = WebRequest.Create(URI); // URI for the GET


            // Set Request credentials

            Request.Credentials = LoginCredentials;


            // Send GET request

            // Recast the response to HttpWebResponse for correct processing

            // Place in a try block to ensure that any errors are caught

            try

            {

                HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();


                // Process the Response as needed

                // This is a generic StreamReader to read the entire response in

                // GetResponseStream can be recast to any of the Stream types

                // Note: Remember to add using System.IO

                StreamReader Reader = new StreamReader(Response.GetResponseStream()); // Read the entire XML response to a string string

                string XMLResponse = Response.StatusCode + " " + Response.StatusDescription + " " + Reader.ReadToEnd();


                // Close Reader

                Reader.Close();


                // Close the response to free up the system resources

                Response.Close();


                MessageBox.Show(XMLResponse);

            }


            catch (WebException ex)

            {

                // Get the web exception type, error number returned here

                MessageBox.Show("WebException:" + ex.Status + " " + ex.Message);

            }


            catch (Exception ex)

            {

                // Get the exception type

                MessageBox.Show("Exception:" + ex.Message);

            }

David J

Regular Participant

* The URI that you have specified- it works fine for me! (same code as i pasted above, just changed to the URI that you pasted: https://api.constantcontact.com/ws/customers/optitex/contacts)


* The URI that I specify- returns the 401. This is my URI: http://api.constantcontact.com/ws/customers/optitex/lists/do-not-mail/members 


Why?..

Anonymous
Not applicable

Please note that you are required to use HTTPS for all your API calls we require SSL.  Your URI is starting with "http://".

Regular Participant

That was it! thank you very much 

Developer Portal

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

Visit Page