C++ Requires Chilkat v11.0.0+
C++
Constant Contact V3: Find a contact list by name
See more Constant Contact Examples
Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.
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 <CkHttpRequest.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;
// Use the full display name. AddParam encodes spaces and punctuation.
const char *listName = "Workshop announcements";
CkHttpRequest query;
query.put_HttpVerb("GET");
query.AddParam("name",listName);
query.AddParam("status","active");
query.AddParam("include_membership_count","active");
success = http.HttpReq("https://api.cc.email/v3/contact_lists",query,resp);
if ((success != true) && (resp.get_StatusCode() <= 0)) {
std::cout << "Look up list: HTTP request failed." << "\r\n";
return;
}
if ((success != true) || (resp.get_StatusCode() != 200)) {
std::cout << "Look up list: HTTP " << resp.get_StatusCode() << " " << resp.bodyStr() << "\r\n";
return;
}
CkJsonObject matches;
success = matches.Load(resp.bodyStr());
if (success != true) {
std::cout << "Expected a JSON object in the response." << "\r\n";
return;
}
if (matches.SizeOfArray("lists") == 0) {
std::cout << "No active email list named " << listName << "\r\n";
return;
}
if ((matches.SizeOfArray("lists") != 1) || (matches.StringOfEquals("lists[0].name",listName,true) != true)) {
std::cout << "Expected one exact list name." << "\r\n";
return;
}
const char *listId = matches.stringOf("lists[0].list_id");
if (listId == "") {
std::cout << "Missing list ID." << "\r\n";
return;
}
std::cout << listName << " | List ID: " << listId << " | Mailable contacts: " << matches.IntOf("lists[0].membership_count") << "\r\n";
}