Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Constant Contact V3: Add existing contacts to a list
See more Constant Contact Examples
Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat Unicode C++ Downloads
#include <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkSecretsW vault;
vault.put_Location(L"local_manager");
vault.put_AutoSplit(true);
CkJsonObjectW entry;
entry.UpdateString(L"appName",L"ConstantContactV3");
entry.UpdateString(L"service",L"OAuth2");
entry.UpdateString(L"username",L"my-account");
CkJsonObjectW tokens;
bool loaded = vault.GetSecretJson(entry,tokens);
if (loaded != true) {
wprintf(L"Run the desktop authorization example using this account and store.\n");
return;
}
if (tokens.JsonTypeOf(L"access_token") != 1) {
wprintf(L"The saved authorization has no access token.\n");
return;
}
CkHttpW http;
http.put_RequireSslCertVerify(true);
http.put_AuthToken(tokens.stringOf(L"access_token"));
CkHttpResponseW resp;
// Use contact IDs from email lookups and the list ID from the selected destination.
CkJsonObjectW body;
body.UpdateString(L"source.contact_ids[0]",L"FIRST_CONTACT_ID");
body.UpdateString(L"source.contact_ids[1]",L"SECOND_CONTACT_ID");
body.UpdateString(L"list_ids[0]",L"DESTINATION_LIST_ID");
success = http.HttpJson(L"POST",L"https://api.cc.email/v3/activities/add_list_memberships",body,L"application/json",resp);
if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
wprintf(L"Could not start the membership activity. HTTP %d\n",resp.get_StatusCode());
return;
}
CkJsonObjectW job;
success = job.Load(resp.bodyStr());
if (success != true) {
wprintf(L"Expected a JSON response.\n");
return;
}
if ((job.JsonTypeOf(L"activity_id") != 1) || (job.StringOfEquals(L"activity_id",L"",true) == true)) {
wprintf(L"Missing activity_id in the response.\n");
return;
}
const wchar_t *activityId = job.stringOf(L"activity_id");
wprintf(L"Submitted activity ID: %s\n",activityId);
http.SetUrlVar(L"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(L"GET",L"https://api.cc.email/v3/activities/{$job}",resp);
if ((success != true) || (resp.get_StatusCode() != 200)) {
wprintf(L"Status check returned HTTP %d. Keep the activity ID and check again later.\n",resp.get_StatusCode());
return;
}
success = job.Load(resp.bodyStr());
if ((success != true) || (job.JsonTypeOf(L"state") != 1)) {
wprintf(L"Missing activity state. Keep the activity ID.\n");
return;
}
const wchar_t *state = job.stringOf(L"state");
if ((state != L"initialized") && (state != L"processing")) {
wprintf(L"Activity state: %s\n",state);
int i = 0;
while ((i < job.SizeOfArray(L"activity_errors"))) {
job.put_I(i);
wprintf(L"%s\n",job.stringOf(L"activity_errors[i]"));
i = i + 1;
}
return;
}
attempt = attempt + 1;
if (attempt < 5) {
http.SleepMs(2000);
}
}
wprintf(L"Still processing. Recheck the activity ID before submitting another request.\n");
}