Sample code for 30+ languages & platforms
Objective-C 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 Objective-C Downloads

Objective-C
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.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];

[http SetUrlVar: @"list" value: @"LIST_ID"];
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://api.cc.email/v3/contacts?lists={$list}&limit=50"];
CkoJsonObject *page = [[CkoJsonObject alloc] init];
int total = 0;
while (([url.Length intValue] > 0)) {
    success = [http HttpNoBody: @"GET" url: [url GetAsString] response: resp];
    if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
        NSLog(@"%@%d",@"Expected HTTP 200. Received ",[resp.StatusCode intValue]);
        if ([resp.StatusCode intValue] == 401) {
            NSLog(@"%@",@"Refresh the saved access token or authorize again.");
        }

        return;
    }

    success = [page Load: resp.BodyStr];
    int count = [[page SizeOfArray: @"contacts"] intValue];
    if ((success != YES) || (count < 0)) {
        NSLog(@"%@",@"Expected a page of contacts.");
        return;
    }

    int i = 0;
    while ((i < count)) {
        page.I = [NSNumber numberWithInt: i];
        NSLog(@"%@%@%@",[page StringOf: @"contacts[i].contact_id"],@"  ",[page StringOf: @"contacts[i].email_address.address"]);
        i = i + 1;
    }

    total = total + count;
    [url Clear];
    if ([[page JsonTypeOf: @"_links.next.href"] intValue] == 1) {
        [url Append: [page StringOf: @"_links.next.href"]];
        //  The documented cursor link is relative to api.cc.email.
        if ([url StartsWith: @"/v3/contacts?" caseSensitive: YES] != YES) {
            NSLog(@"%@",@"Unexpected pagination link. Stopped before following it.");
            return;
        }

        [url Prepend: @"https://api.cc.email"];
    }

}

NSLog(@"%@%d",@"List members read: ",total);