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

Constant Contact V3: Create a contact list

See more Constant Contact Examples

Create a list with a unique name and a short description. Save the returned list_id with your application’s list record and use it in the membership examples. The new list starts empty. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

# Assumes Chilkat is unlocked. See Global Unlock Sample.
my $vault = Chilkat2::Secrets->new;
$vault->Location('local_manager');
$vault->AutoSplit(1);
my $entry = Chilkat2::JsonObject->new;
$entry->UpdateString('appName', 'ConstantContactV3');
$entry->UpdateString('service', 'OAuth2');
$entry->UpdateString('username', 'my-account');
my $tokens = Chilkat2::JsonObject->new;
my $loaded = $vault->GetSecretJson($entry, $tokens);
if (!$loaded) {
    print "Run the desktop authorization example using this account and store.\n";
    exit;
}

if ($tokens->JsonTypeOf('access_token') != 1) {
    print "The saved authorization has no access token.\n";
    exit;
}

my $http = Chilkat2::Http->new;
$http->RequireSslCertVerify(1);
$http->AuthToken($tokens->StringOf('access_token'));
my $resp = Chilkat2::HttpResponse->new;

my $body = Chilkat2::JsonObject->new;
$body->UpdateString('name', 'Workshop announcements');
$body->UpdateString('description', 'People who requested workshop updates');
my $success = $http->HttpJson('POST', 'https://api.cc.email/v3/contact_lists', $body, 'application/json', $resp);
if ($resp->StatusCode == 409) {
    print "A conflicting list already exists. Check the existing lists before retrying.\n";
    exit;
}

if ((!$success) || ($resp->StatusCode != 201)) {
    print 'Expected HTTP 201. Received ', $resp->StatusCode, "\n";
    if ($resp->StatusCode == 401) {
        print "Refresh the saved access token or authorize again.\n";
    }

    exit;
}

my $created = Chilkat2::JsonObject->new;
$success = $created->Load($resp->BodyStr);
if (!$success) {
    print "Expected a JSON response.\n";
    exit;
}

if (($created->JsonTypeOf('list_id') != 1) || ($created->StringOfEquals('list_id', '', 1))) {
    print "Missing list_id in the response.\n";
    exit;
}

print 'Created ', $created->StringOf('name'), '. List ID: ', $created->StringOf('list_id'), "\n";