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

Constant Contact V3: Find a contact by email address

See more Constant Contact Examples

Look up an exact email address across contact states, including deleted contacts. Pass the address as a query parameter so a literal plus sign survives URL encoding. Read the stored contact ID, email permission, and deletion marker. An empty contacts array means no match. 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();

  final query = CkHttpRequest();
  query.httpVerb = 'GET';
  query.addParam('email', 'reader+newsletter@example.com');
  query.addParam('status', 'all');
  success = true;
  try {
    http.httpReq('https://api.cc.email/v3/contacts', query, 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;
  }

  final reply = CkJsonObject();
  try {
    reply.load(resp.bodyStr);
  } on ChilkatException {
    print('Expected a JSON response.');
    return;
  }

  final count = reply.sizeOfArray('contacts');
  if (count < 0) {
    print('The reply contained no contacts array.');
    return;
  }

  if (count == 0) {
    print('No contact has that email address.');
    return;
  }

  var i = 0;
  while (i < count) {
    reply.i = i;
    print('Contact ID: ${reply.stringOf('contacts[i].contact_id')}');
    print('Email permission: ${reply.stringOf('contacts[i].email_address.permission_to_send')}');
    if (reply.jsonTypeOf('contacts[i].deleted_at') == 1) {
      print('Deleted at: ${reply.stringOf('contacts[i].deleted_at')}');
    }

    i++;
  }
}