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

C++
#include <CkSecrets.h>
#include <CkJsonObject.h>
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    if (tokens.JsonTypeOf("access_token") != 1) {
        std::cout << "The saved authorization has no access token." << "\r\n";
        return;
    }

    CkHttp http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(tokens.stringOf("access_token"));
    CkHttpResponse resp;

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

            return;
        }

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

        int i = 0;
        while ((i < count)) {
            page.put_I(i);
            std::cout << page.stringOf("contacts[i].contact_id") << "  " << page.stringOf("contacts[i].email_address.address") << "\r\n";
            i = i + 1;
        }

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

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

    }

    std::cout << "List members read: " << total << "\r\n";
    }