C++ Requires Chilkat v11.0.0+
C++
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 C++ Downloads
#include <CkSecrets.h>
#include <CkJsonObject.h>
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkSecrets vault;
vault.put_Location("local_manager");
vault.put_AutoSplit(true);
CkJsonObject entry;
entry.UpdateString("appName","ConstantContactV3");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","my-account");
CkJsonObject tokens;
bool loaded = vault.GetSecretJson(entry,tokens);
if (loaded != true) {
std::cout << "Run the desktop authorization example using this account and store." << "\r\n";
return;
}
if (tokens.JsonTypeOf("access_token") != 1) {
std::cout << "The saved authorization has no access token." << "\r\n";
return;
}
CkHttp http;
http.put_RequireSslCertVerify(true);
http.put_AuthToken(tokens.stringOf("access_token"));
CkHttpResponse resp;
const char *campaignName = "Workshop newsletter";
const char *campaignId = "";
CkStringBuilder url;
url.Append("https://api.cc.email/v3/emails?limit=50");
CkJsonObject page;
while (((url.get_Length() > 0) && (campaignId == ""))) {
success = http.HttpNoBody("GET",url.getAsString(),resp);
if ((success != true) && (resp.get_StatusCode() <= 0)) {
std::cout << "Read campaigns: HTTP request failed." << "\r\n";
return;
}
if ((success != true) || (resp.get_StatusCode() != 200)) {
std::cout << "Read campaigns: HTTP " << resp.get_StatusCode() << " " << resp.bodyStr() << "\r\n";
return;
}
success = page.Load(resp.bodyStr());
int count = page.SizeOfArray("campaigns");
if ((success != true) || (count < 0)) {
std::cout << "Expected a page of campaigns." << "\r\n";
return;
}
int i = 0;
while (((i < count) && (campaignId == ""))) {
page.put_I(i);
if (page.StringOfEquals("campaigns[i].name",campaignName,true) == true) {
campaignId = page.stringOf("campaigns[i].campaign_id");
}
i = i + 1;
}
url.Clear();
if ((campaignId == "") && (page.JsonTypeOf("_links.next.href") == 1)) {
url.Append(page.stringOf("_links.next.href"));
if (url.StartsWith("/v3/emails?",true) != true) {
std::cout << "Unexpected campaign pagination link." << "\r\n";
return;
}
url.Prepend("https://api.cc.email");
}
}
if (campaignId == "") {
std::cout << "No campaign named " << campaignName << "\r\n";
return;
}
http.SetUrlVar("campaign",campaignId);
success = http.HttpNoBody("GET","https://api.cc.email/v3/emails/{$campaign}",resp);
if ((success != true) && (resp.get_StatusCode() <= 0)) {
std::cout << "Read campaign: HTTP request failed." << "\r\n";
return;
}
if ((success != true) || (resp.get_StatusCode() != 200)) {
std::cout << "Read campaign: HTTP " << resp.get_StatusCode() << " " << resp.bodyStr() << "\r\n";
return;
}
CkJsonObject campaign;
success = campaign.Load(resp.bodyStr());
if (success != true) {
std::cout << "Expected a JSON object in the response." << "\r\n";
return;
}
// The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
CkJsonObject primary;
success = campaign.FindRecord2("campaign_activities","role","primary_email",true,primary);
if ((success != true) || (primary.JsonTypeOf("campaign_activity_id") != 1)) {
std::cout << "No primary email activity ID in the response." << "\r\n";
return;
}
const char *selectedCampaign = campaign.stringOf("campaign_id");
const char *selectedActivity = primary.stringOf("campaign_activity_id");
if ((selectedCampaign == "") || (selectedActivity == "")) {
std::cout << "Missing campaign or activity ID." << "\r\n";
return;
}
std::cout << "Campaign ID: " << selectedCampaign << "\r\n";
std::cout << "Primary activity ID: " << selectedActivity << "\r\n";
CkJsonObject selection;
selection.UpdateString("name",campaign.stringOf("name"));
selection.UpdateString("campaign_id",selectedCampaign);
selection.UpdateString("campaign_activity_id",selectedActivity);
success = selection.WriteFile("newsletter-selection.json");
if (success != true) {
std::cout << "Could not save newsletter-selection.json. Keep the IDs printed above." << "\r\n";
return;
}
std::cout << "Saved newsletter-selection.json." << "\r\n";
}