Java Requires Chilkat v11.0.0+
Java
Constant Contact V3: Find a contact by email address
See more Constant Contact Examples
Look up an exact email address across contact states, including deleted contacts. Pass the address as a query parameter so a literal plus sign survives URL encoding. Read the stored contact ID, email permission, and deletion marker. An empty contacts array means no match. 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.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();
HttpRequest query = new HttpRequest();
query.setHttpVerb("GET");
query.addParam("email", "reader+newsletter@example.com");
query.addParam("status", "all");
try {
http.httpReq("https://api.cc.email/v3/contacts", query, 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;
}
JsonObject reply = new JsonObject();
try {
reply.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON response.");
return;
}
int count = reply.sizeOfArray("contacts");
if (count < 0) {
System.out.println("The reply contained no contacts array.");
return;
}
if (count == 0) {
System.out.println("No contact has that email address.");
return;
}
int i = 0;
while (i < count) {
reply.setI(i);
System.out.println("Contact ID: " + reply.stringOf("contacts[i].contact_id"));
System.out.println("Email permission: " + reply.stringOf("contacts[i].email_address.permission_to_send"));
if (reply.jsonTypeOf("contacts[i].deleted_at") == 1) {
System.out.println("Deleted at: " + reply.stringOf("contacts[i].deleted_at"));
}
i = i + 1;
}
}
}