Java Requires Chilkat v11.0.0+
Java
Constant Contact V3: Restore a deleted contact to a list
See more Constant Contact Examples
Look up the deleted contact by email, then restore it by adding its existing ID to a chosen list. Constant Contact documents this membership activity as a restoration method. It changes only membership, preserves the saved profile, and makes no email-permission claim. Select the intended destination list explicitly; previous memberships were removed by deletion. 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", "deleted");
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 matches = new JsonObject();
try {
matches.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON response.");
return;
}
if ((matches.sizeOfArray("contacts") != 1) || (matches.jsonTypeOf("contacts[0].deleted_at") != 1)) {
System.out.println("Expected one deleted contact for this email. No restoration submitted.");
return;
}
if ((matches.jsonTypeOf("contacts[0].contact_id") != 1) || (matches.stringOfEquals("contacts[0].contact_id", "", true))) {
System.out.println("Missing contacts[0].contact_id in the response.");
return;
}
JsonObject body = new JsonObject();
body.updateString("source.contact_ids[0]", matches.stringOf("contacts[0].contact_id"));
body.updateString("list_ids[0]", "DESTINATION_LIST_ID");
try {
http.httpJson("POST", "https://api.cc.email/v3/activities/add_list_memberships", body, "application/json", resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300))) {
System.out.println("Could not start the membership activity. HTTP " + resp.getStatusCode());
return;
}
JsonObject job = new JsonObject();
try {
job.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON response.");
return;
}
if ((job.jsonTypeOf("activity_id") != 1) || (job.stringOfEquals("activity_id", "", true))) {
System.out.println("Missing activity_id in the response.");
return;
}
String activityId = job.stringOf("activity_id");
System.out.println("Submitted activity ID: " + activityId);
http.setUrlVar("job", activityId);
// Poll briefly. A longer-running job can be checked later using the same ID.
int attempt = 0;
while (attempt < 5) {
try {
http.httpNoBody("GET", "https://api.cc.email/v3/activities/{$job}", resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (resp.getStatusCode() != 200)) {
System.out.println("Status check returned HTTP " + resp.getStatusCode() + ". Keep the activity ID and check again later.");
return;
}
try {
job.load(resp.getBodyStr());
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (job.jsonTypeOf("state") != 1)) {
System.out.println("Missing activity state. Keep the activity ID.");
return;
}
String state = job.stringOf("state");
if ((!"initialized".equals(state)) && (!"processing".equals(state))) {
System.out.println("Activity state: " + state);
int i = 0;
while (i < job.sizeOfArray("activity_errors")) {
job.setI(i);
System.out.println(job.stringOf("activity_errors[i]"));
i = i + 1;
}
return;
}
attempt = attempt + 1;
if (attempt < 5) {
http.sleepMs(2000);
}
}
System.out.println("Still processing. Recheck the activity ID before submitting another request.");
}
}