C++ Requires Chilkat v11.0.0+
C++
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 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;
CkHttpRequest query;
query.put_HttpVerb("GET");
query.AddParam("email","reader+newsletter@example.com");
query.AddParam("status","deleted");
success = http.HttpReq("https://api.cc.email/v3/contacts",query,resp);
if ((success != true) || (resp.get_StatusCode() != 200)) {
std::cout << "Expected HTTP 200. Received " << resp.get_StatusCode() << "\r\n";
if (resp.get_StatusCode() == 401) {
std::cout << "Refresh the saved access token or authorize again." << "\r\n";
}
return;
}
CkJsonObject matches;
success = matches.Load(resp.bodyStr());
if (success != true) {
std::cout << "Expected a JSON response." << "\r\n";
return;
}
if ((matches.SizeOfArray("contacts") != 1) || (matches.JsonTypeOf("contacts[0].deleted_at") != 1)) {
std::cout << "Expected one deleted contact for this email. No restoration submitted." << "\r\n";
return;
}
if ((matches.JsonTypeOf("contacts[0].contact_id") != 1) || (matches.StringOfEquals("contacts[0].contact_id","",true) == true)) {
std::cout << "Missing contacts[0].contact_id in the response." << "\r\n";
return;
}
CkJsonObject body;
body.UpdateString("source.contact_ids[0]",matches.stringOf("contacts[0].contact_id"));
body.UpdateString("list_ids[0]","DESTINATION_LIST_ID");
success = http.HttpJson("POST","https://api.cc.email/v3/activities/add_list_memberships",body,"application/json",resp);
if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
std::cout << "Could not start the membership activity. HTTP " << resp.get_StatusCode() << "\r\n";
return;
}
CkJsonObject job;
success = job.Load(resp.bodyStr());
if (success != true) {
std::cout << "Expected a JSON response." << "\r\n";
return;
}
if ((job.JsonTypeOf("activity_id") != 1) || (job.StringOfEquals("activity_id","",true) == true)) {
std::cout << "Missing activity_id in the response." << "\r\n";
return;
}
const char *activityId = job.stringOf("activity_id");
std::cout << "Submitted activity ID: " << activityId << "\r\n";
http.SetUrlVar("job",activityId);
// Poll briefly. A longer-running job can be checked later using the same ID.
int attempt = 0;
while ((attempt < 5)) {
success = http.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",resp);
if ((success != true) || (resp.get_StatusCode() != 200)) {
std::cout << "Status check returned HTTP " << resp.get_StatusCode() << ". Keep the activity ID and check again later." << "\r\n";
return;
}
success = job.Load(resp.bodyStr());
if ((success != true) || (job.JsonTypeOf("state") != 1)) {
std::cout << "Missing activity state. Keep the activity ID." << "\r\n";
return;
}
const char *state = job.stringOf("state");
if ((state != "initialized") && (state != "processing")) {
std::cout << "Activity state: " << state << "\r\n";
int i = 0;
while ((i < job.SizeOfArray("activity_errors"))) {
job.put_I(i);
std::cout << job.stringOf("activity_errors[i]") << "\r\n";
i = i + 1;
}
return;
}
attempt = attempt + 1;
if (attempt < 5) {
http.SleepMs(2000);
}
}
std::cout << "Still processing. Recheck the activity ID before submitting another request." << "\r\n";
}