Sample code for 30+ languages & platforms
CkJava 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 CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

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

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkSecrets vault = new CkSecrets();
    vault.put_Location("local_manager");
    vault.put_AutoSplit(true);
    CkJsonObject entry = new CkJsonObject();
    entry.UpdateString("appName","ConstantContactV3");
    entry.UpdateString("service","OAuth2");
    entry.UpdateString("username","my-account");
    CkJsonObject tokens = new CkJsonObject();
    boolean loaded = vault.GetSecretJson(entry,tokens);
    if (loaded != true) {
        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;
        }

    CkHttp http = new CkHttp();
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(tokens.stringOf("access_token"));
    CkHttpResponse resp = new CkHttpResponse();

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

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

    CkJsonObject matches = new CkJsonObject();
    success = matches.Load(resp.bodyStr());
    if (success != true) {
        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) != true)) {
        System.out.println("Expected one exact list name.");
        return;
        }

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

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