Java Requires Chilkat v11.0.0+
Java
Constant Contact V3: Read every page of list members
See more Constant Contact Examples
Filter the contacts collection with lists and a list ID. To query multiple lists together, supply their comma-separated IDs. Follow the returned cursor links and report the total after the last page. The default contacts query excludes deleted records. A list member can still have unsubscribed email permission. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Secrets;
import com.chilkatsoft.chilkat2.StringBuilder;
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();
http.setUrlVar("list", "LIST_ID");
StringBuilder url = new StringBuilder();
url.append("https://api.cc.email/v3/contacts?lists={$list}&limit=50");
JsonObject page = new JsonObject();
int total = 0;
while (url.getLength() > 0) {
try {
http.httpNoBody("GET", url.getAsString(), resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (resp.getStatusCode() != 200)) {
System.out.println("Expected HTTP 200. Received " + resp.getStatusCode());
if (resp.getStatusCode() == 401) {
System.out.println("Refresh the saved access token or authorize again.");
}
return;
}
try {
page.load(resp.getBodyStr());
success = true;
} catch (ChilkatException e) {
success = false;
}
int count = page.sizeOfArray("contacts");
if ((!success) || (count < 0)) {
System.out.println("Expected a page of contacts.");
return;
}
int i = 0;
while (i < count) {
page.setI(i);
System.out.println(page.stringOf("contacts[i].contact_id") + " " + page.stringOf("contacts[i].email_address.address"));
i = i + 1;
}
total = total + count;
url.clear();
if (page.jsonTypeOf("_links.next.href") == 1) {
url.append(page.stringOf("_links.next.href"));
// The documented cursor link is relative to api.cc.email.
if (!url.startsWith("/v3/contacts?", true)) {
System.out.println("Unexpected pagination link. Stopped before following it.");
return;
}
url.prepend("https://api.cc.email");
}
}
System.out.println("List members read: " + total);
}
}