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 <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkSecretsW vault;
    vault.put_Location(L"local_manager");
    vault.put_AutoSplit(true);
    CkJsonObjectW entry;
    entry.UpdateString(L"appName",L"ConstantContactV3");
    entry.UpdateString(L"service",L"OAuth2");
    entry.UpdateString(L"username",L"my-account");
    CkJsonObjectW tokens;
    bool loaded = vault.GetSecretJson(entry,tokens);
    if (loaded != true) {
        wprintf(L"Run the desktop authorization example using this account and store.\n");
        return;
    }

    if (tokens.JsonTypeOf(L"access_token") != 1) {
        wprintf(L"The saved authorization has no access token.\n");
        return;
    }

    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(tokens.stringOf(L"access_token"));
    CkHttpResponseW resp;

    http.SetUrlVar(L"list",L"LIST_ID");
    CkStringBuilderW url;
    url.Append(L"https://api.cc.email/v3/contacts?lists={$list}&limit=50");
    CkJsonObjectW page;
    int total = 0;
    while ((url.get_Length() > 0)) {
        success = http.HttpNoBody(L"GET",url.getAsString(),resp);
        if ((success != true) || (resp.get_StatusCode() != 200)) {
            wprintf(L"Expected HTTP 200. Received %d\n",resp.get_StatusCode());
            if (resp.get_StatusCode() == 401) {
                wprintf(L"Refresh the saved access token or authorize again.\n");
            }

            return;
        }

        success = page.Load(resp.bodyStr());
        int count = page.SizeOfArray(L"contacts");
        if ((success != true) || (count < 0)) {
            wprintf(L"Expected a page of contacts.\n");
            return;
        }

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

        total = total + count;
        url.Clear();
        if (page.JsonTypeOf(L"_links.next.href") == 1) {
            url.Append(page.stringOf(L"_links.next.href"));
            //  The documented cursor link is relative to api.cc.email.
            if (url.StartsWith(L"/v3/contacts?",true) != true) {
                wprintf(L"Unexpected pagination link. Stopped before following it.\n");
                return;
            }

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

    }

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