Java Requires Chilkat v11.0.0+
Java
Constant Contact V3: Find a campaign and its primary email activity
See more Constant Contact Examples
Enter the unique campaign name from the account. Follow the campaign collection’s cursor until the name matches, then fetch that campaign’s details. Select the activity whose role is primary_email. A permalink or resend can appear first.
The example writes the two IDs to newsletter-selection.json, the same file produced by the draft example. Run this lookup when selecting an existing newsletter for review or scheduling.
Authorize the application using the same account and 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();
String campaignName = "Workshop newsletter";
String campaignId = "";
StringBuilder url = new StringBuilder();
url.append("https://api.cc.email/v3/emails?limit=50");
JsonObject page = new JsonObject();
while ((url.getLength() > 0) && ("".equals(campaignId))) {
try {
http.httpNoBody("GET", url.getAsString(), resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) && (resp.getStatusCode() <= 0)) {
System.out.println("Read campaigns: HTTP request failed.");
return;
}
if ((!success) || (resp.getStatusCode() != 200)) {
System.out.println("Read campaigns: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
return;
}
try {
page.load(resp.getBodyStr());
success = true;
} catch (ChilkatException e) {
success = false;
}
int count = page.sizeOfArray("campaigns");
if ((!success) || (count < 0)) {
System.out.println("Expected a page of campaigns.");
return;
}
int i = 0;
while ((i < count) && ("".equals(campaignId))) {
page.setI(i);
if (page.stringOfEquals("campaigns[i].name", campaignName, true)) {
campaignId = page.stringOf("campaigns[i].campaign_id");
}
i = i + 1;
}
url.clear();
if (("".equals(campaignId)) && (page.jsonTypeOf("_links.next.href") == 1)) {
url.append(page.stringOf("_links.next.href"));
if (!url.startsWith("/v3/emails?", true)) {
System.out.println("Unexpected campaign pagination link.");
return;
}
url.prepend("https://api.cc.email");
}
}
if ("".equals(campaignId)) {
System.out.println("No campaign named " + campaignName);
return;
}
http.setUrlVar("campaign", campaignId);
try {
http.httpNoBody("GET", "https://api.cc.email/v3/emails/{$campaign}", resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) && (resp.getStatusCode() <= 0)) {
System.out.println("Read campaign: HTTP request failed.");
return;
}
if ((!success) || (resp.getStatusCode() != 200)) {
System.out.println("Read campaign: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
return;
}
JsonObject campaign = new JsonObject();
try {
campaign.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON object in the response.");
return;
}
// The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
JsonObject primary = new JsonObject();
try {
campaign.findRecord2("campaign_activities", "role", "primary_email", true, primary);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (primary.jsonTypeOf("campaign_activity_id") != 1)) {
System.out.println("No primary email activity ID in the response.");
return;
}
String selectedCampaign = campaign.stringOf("campaign_id");
String selectedActivity = primary.stringOf("campaign_activity_id");
if (("".equals(selectedCampaign)) || ("".equals(selectedActivity))) {
System.out.println("Missing campaign or activity ID.");
return;
}
System.out.println("Campaign ID: " + selectedCampaign);
System.out.println("Primary activity ID: " + selectedActivity);
JsonObject selection = new JsonObject();
selection.updateString("name", campaign.stringOf("name"));
selection.updateString("campaign_id", selectedCampaign);
selection.updateString("campaign_activity_id", selectedActivity);
try {
selection.writeFile("newsletter-selection.json");
} catch (ChilkatException e) {
System.out.println("Could not save newsletter-selection.json. Keep the IDs printed above.");
return;
}
System.out.println("Saved newsletter-selection.json.");
}
}