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

Unicode C
#include <C_CkSecretsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSecretsW vault;
    HCkJsonObjectW entry;
    HCkJsonObjectW tokens;
    BOOL loaded;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkStringBuilderW url;
    HCkJsonObjectW page;
    int total;
    int count;
    int i;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    vault = CkSecretsW_Create();
    CkSecretsW_putLocation(vault,L"local_manager");
    CkSecretsW_putAutoSplit(vault,TRUE);
    entry = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(entry,L"appName",L"ConstantContactV3");
    CkJsonObjectW_UpdateString(entry,L"service",L"OAuth2");
    CkJsonObjectW_UpdateString(entry,L"username",L"my-account");
    tokens = CkJsonObjectW_Create();
    loaded = CkSecretsW_GetSecretJson(vault,entry,tokens);
    if (loaded != TRUE) {
        wprintf(L"Run the desktop authorization example using this account and store.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        return;
    }

    if (CkJsonObjectW_JsonTypeOf(tokens,L"access_token") != 1) {
        wprintf(L"The saved authorization has no access token.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        return;
    }

    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(tokens,L"access_token"));
    resp = CkHttpResponseW_Create();

    CkHttpW_SetUrlVar(http,L"list",L"LIST_ID");
    url = CkStringBuilderW_Create();
    CkStringBuilderW_Append(url,L"https://api.cc.email/v3/contacts?lists={$list}&limit=50");
    page = CkJsonObjectW_Create();
    total = 0;
    while ((CkStringBuilderW_getLength(url) > 0)) {
        success = CkHttpW_HttpNoBody(http,L"GET",CkStringBuilderW_getAsString(url),resp);
        if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) != 200)) {
            wprintf(L"Expected HTTP 200. Received %d\n",CkHttpResponseW_getStatusCode(resp));
            if (CkHttpResponseW_getStatusCode(resp) == 401) {
                wprintf(L"Refresh the saved access token or authorize again.\n");
            }

            CkSecretsW_Dispose(vault);
            CkJsonObjectW_Dispose(entry);
            CkJsonObjectW_Dispose(tokens);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkStringBuilderW_Dispose(url);
            CkJsonObjectW_Dispose(page);
            return;
        }

        success = CkJsonObjectW_Load(page,CkHttpResponseW_bodyStr(resp));
        count = CkJsonObjectW_SizeOfArray(page,L"contacts");
        if ((success != TRUE) || (count < 0)) {
            wprintf(L"Expected a page of contacts.\n");
            CkSecretsW_Dispose(vault);
            CkJsonObjectW_Dispose(entry);
            CkJsonObjectW_Dispose(tokens);
            CkHttpW_Dispose(http);
            CkHttpResponseW_Dispose(resp);
            CkStringBuilderW_Dispose(url);
            CkJsonObjectW_Dispose(page);
            return;
        }

        i = 0;
        while ((i < count)) {
            CkJsonObjectW_putI(page,i);
            wprintf(L"%s  %s\n",CkJsonObjectW_stringOf(page,L"contacts[i].contact_id"),CkJsonObjectW_stringOf(page,L"contacts[i].email_address.address"));
            i = i + 1;
        }

        total = total + count;
        CkStringBuilderW_Clear(url);
        if (CkJsonObjectW_JsonTypeOf(page,L"_links.next.href") == 1) {
            CkStringBuilderW_Append(url,CkJsonObjectW_stringOf(page,L"_links.next.href"));
            //  The documented cursor link is relative to api.cc.email.
            if (CkStringBuilderW_StartsWith(url,L"/v3/contacts?",TRUE) != TRUE) {
                wprintf(L"Unexpected pagination link. Stopped before following it.\n");
                CkSecretsW_Dispose(vault);
                CkJsonObjectW_Dispose(entry);
                CkJsonObjectW_Dispose(tokens);
                CkHttpW_Dispose(http);
                CkHttpResponseW_Dispose(resp);
                CkStringBuilderW_Dispose(url);
                CkJsonObjectW_Dispose(page);
                return;
            }

            CkStringBuilderW_Prepend(url,L"https://api.cc.email");
        }

    }

    wprintf(L"List members read: %d\n",total);


    CkSecretsW_Dispose(vault);
    CkJsonObjectW_Dispose(entry);
    CkJsonObjectW_Dispose(tokens);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkStringBuilderW_Dispose(url);
    CkJsonObjectW_Dispose(page);

    }