Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final vault = CkSecrets();
vault.location = 'local_manager';
vault.autoSplit = true;
final entry = CkJsonObject();
entry.updateString('appName', 'ConstantContactV3');
entry.updateString('service', 'OAuth2');
entry.updateString('username', 'my-account');
final tokens = CkJsonObject();
try {
vault.getSecretJson(entry, tokens);
} on ChilkatException {
print('Run the desktop authorization example using this account and store.');
return;
}
if (tokens.jsonTypeOf('access_token') != 1) {
print('The saved authorization has no access token.');
return;
}
final http = CkHttp();
http.requireSslCertVerify = true;
http.authToken = tokens.stringOf('access_token');
final resp = CkHttpResponse();
// Use contact IDs from email lookups and the list ID from the selected destination.
final body = CkJsonObject();
body.updateString('source.contact_ids[0]', 'FIRST_CONTACT_ID');
body.updateString('source.contact_ids[1]', 'SECOND_CONTACT_ID');
body.updateString('list_ids[0]', 'DESTINATION_LIST_ID');
success = true;
try {
http.httpJson('POST', 'https://api.cc.email/v3/activities/add_list_memberships', body, 'application/json', resp);
} on ChilkatException {
success = false;
}
if ((!success) || ((resp.statusCode < 200) || (resp.statusCode >= 300))) {
print('Could not start the membership activity. HTTP ${resp.statusCode}');
return;
}
final job = CkJsonObject();
success = true;
try {
job.load(resp.bodyStr);
} on ChilkatException {
success = false;
}
if (!success) {
print('Expected a JSON response.');
return;
}
if ((job.jsonTypeOf('activity_id') != 1) || (job.stringOfEquals('activity_id', '', true))) {
print('Missing activity_id in the response.');
return;
}
final activityId = job.stringOf('activity_id');
print('Submitted activity ID: $activityId');
http.setUrlVar('job', activityId);
// Poll briefly. A longer-running job can be checked later using the same ID.
var attempt = 0;
while (attempt < 5) {
success = true;
try {
http.httpNoBody('GET', 'https://api.cc.email/v3/activities/{\$job}', resp);
} on ChilkatException {
success = false;
}
if ((!success) || (resp.statusCode != 200)) {
print('Status check returned HTTP ${resp.statusCode}. Keep the activity ID and check again later.');
return;
}
success = true;
try {
job.load(resp.bodyStr);
} on ChilkatException {
success = false;
}
if ((!success) || (job.jsonTypeOf('state') != 1)) {
print('Missing activity state. Keep the activity ID.');
return;
}
final state = job.stringOf('state');
if ((state != 'initialized') && (state != 'processing')) {
print('Activity state: $state');
var i = 0;
while (i < job.sizeOfArray('activity_errors')) {
job.i = i;
print(job.stringOf('activity_errors[i]'));
i++;
}
return;
}
attempt++;
if (attempt < 5) {
http.sleepMs(2000);
}
}
print('Still processing. Recheck the activity ID before submitting another request.');
}