Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Constant Contact V3: Find a contact list by name

See more Constant Contact Examples

Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Objective-C Downloads

Objective-C
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <NSString.h>
#import <CkoHttpRequest.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoSecrets *vault = [[CkoSecrets alloc] init];
vault.Location = @"local_manager";
vault.AutoSplit = YES;
CkoJsonObject *entry = [[CkoJsonObject alloc] init];
[entry UpdateString: @"appName" value: @"ConstantContactV3"];
[entry UpdateString: @"service" value: @"OAuth2"];
[entry UpdateString: @"username" value: @"my-account"];
CkoJsonObject *tokens = [[CkoJsonObject alloc] init];
BOOL loaded = [vault GetSecretJson: entry jsonOut: tokens];
if (loaded != YES) {
    NSLog(@"%@",@"Run the desktop authorization example using this account and store.");
    return;
}

if ([[tokens JsonTypeOf: @"access_token"] intValue] != 1) {
    NSLog(@"%@",@"The saved authorization has no access token.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [tokens StringOf: @"access_token"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];

//  Use the full display name. AddParam encodes spaces and punctuation.
NSString *listName = @"Workshop announcements";
CkoHttpRequest *query = [[CkoHttpRequest alloc] init];
query.HttpVerb = @"GET";
[query AddParam: @"name" value: listName];
[query AddParam: @"status" value: @"active"];
[query AddParam: @"include_membership_count" value: @"active"];
success = [http HttpReq: @"https://api.cc.email/v3/contact_lists" request: query response: resp];
if ((success != YES) && ([resp.StatusCode intValue] <= 0)) {
    NSLog(@"%@",@"Look up list: HTTP request failed.");
    return;
}

if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
    NSLog(@"%@%d%@%@",@"Look up list: HTTP ",[resp.StatusCode intValue],@" ",resp.BodyStr);
    return;
}

CkoJsonObject *matches = [[CkoJsonObject alloc] init];
success = [matches Load: resp.BodyStr];
if (success != YES) {
    NSLog(@"%@",@"Expected a JSON object in the response.");
    return;
}

if ([[matches SizeOfArray: @"lists"] intValue] == 0) {
    NSLog(@"%@%@",@"No active email list named ",listName);
    return;
}

if (([[matches SizeOfArray: @"lists"] intValue] != 1) || ([matches StringOfEquals: @"lists[0].name" value: listName caseSensitive: YES] != YES)) {
    NSLog(@"%@",@"Expected one exact list name.");
    return;
}

NSString *listId = [matches StringOf: @"lists[0].list_id"];
if (listId == @"") {
    NSLog(@"%@",@"Missing list ID.");
    return;
}

NSLog(@"%@%@%@%@%d",listName,@" | List ID: ",listId,@" | Mailable contacts: ",[[matches IntOf: @"lists[0].membership_count"] intValue]);