Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

Constant Contact V3: Read every page of list members

See more Constant Contact Examples

Filter the contacts collection with lists and a list ID. To query multiple lists together, supply their comma-separated IDs. Follow the returned cursor links and report the total after the last page. The default contacts query excludes deleted records. A list member can still have unsubscribed email permission. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat Dart Downloads

Dart
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();

  http.setUrlVar('list', 'LIST_ID');
  final url = CkStringBuilder();
  url.append('https://api.cc.email/v3/contacts?lists={\$list}&limit=50');
  final page = CkJsonObject();
  var total = 0;
  while (url.length > 0) {
    success = true;
    try {
      http.httpNoBody('GET', url.getAsString(), resp);
    } on ChilkatException {
      success = false;
    }
    if ((!success) || (resp.statusCode != 200)) {
      print('Expected HTTP 200. Received ${resp.statusCode}');
      if (resp.statusCode == 401) {
        print('Refresh the saved access token or authorize again.');
      }

      return;
    }

    success = true;
    try {
      page.load(resp.bodyStr);
    } on ChilkatException {
      success = false;
    }
    final count = page.sizeOfArray('contacts');
    if ((!success) || (count < 0)) {
      print('Expected a page of contacts.');
      return;
    }

    var i = 0;
    while (i < count) {
      page.i = i;
      print('${page.stringOf('contacts[i].contact_id')}  ${page.stringOf('contacts[i].email_address.address')}');
      i++;
    }

    total += count;
    url.clear();
    if (page.jsonTypeOf('_links.next.href') == 1) {
      url.append(page.stringOf('_links.next.href'));
      // The documented cursor link is relative to api.cc.email.
      if (!url.startsWith('/v3/contacts?', true)) {
        print('Unexpected pagination link. Stopped before following it.');
        return;
      }

      url.prepend('https://api.cc.email');
    }
  }

  print('List members read: $total');
}