Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Constant Contact V3: Find a contact list by name

See more Constant Contact Examples

Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Secrets;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        Secrets vault = new Secrets();
        vault.setLocation("local_manager");
        vault.setAutoSplit(true);
        JsonObject entry = new JsonObject();
        entry.updateString("appName", "ConstantContactV3");
        entry.updateString("service", "OAuth2");
        entry.updateString("username", "my-account");
        JsonObject tokens = new JsonObject();
        try {
            vault.getSecretJson(entry, tokens);
        } catch (ChilkatException e) {
            System.out.println("Run the desktop authorization example using this account and store.");
            return;
        }

        if (tokens.jsonTypeOf("access_token") != 1) {
            System.out.println("The saved authorization has no access token.");
            return;
        }

        Http http = new Http();
        http.setRequireSslCertVerify(true);
        http.setAuthToken(tokens.stringOf("access_token"));
        HttpResponse resp = new HttpResponse();

        // Use the full display name. AddParam encodes spaces and punctuation.
        String listName = "Workshop announcements";
        HttpRequest query = new HttpRequest();
        query.setHttpVerb("GET");
        query.addParam("name", listName);
        query.addParam("status", "active");
        query.addParam("include_membership_count", "active");
        try {
            http.httpReq("https://api.cc.email/v3/contact_lists", query, resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if ((!success) && (resp.getStatusCode() <= 0)) {
            System.out.println("Look up list: HTTP request failed.");
            return;
        }

        if ((!success) || (resp.getStatusCode() != 200)) {
            System.out.println("Look up list: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
            return;
        }

        JsonObject matches = new JsonObject();
        try {
            matches.load(resp.getBodyStr());
        } catch (ChilkatException e) {
            System.out.println("Expected a JSON object in the response.");
            return;
        }

        if (matches.sizeOfArray("lists") == 0) {
            System.out.println("No active email list named " + listName);
            return;
        }

        if ((matches.sizeOfArray("lists") != 1) || (!matches.stringOfEquals("lists[0].name", listName, true))) {
            System.out.println("Expected one exact list name.");
            return;
        }

        String listId = matches.stringOf("lists[0].list_id");
        if ("".equals(listId)) {
            System.out.println("Missing list ID.");
            return;
        }

        System.out.println(listName + " | List ID: " + listId + " | Mailable contacts: " + matches.intOf("lists[0].membership_count"));
    }
}