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

Constant Contact V3: Create a contact list

See more Constant Contact Examples

Create a list with a unique name and a short description. Save the returned list_id with your application’s list record and use it in the membership examples. The new list starts empty. 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>

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;

    CkJsonObject body;
    body.UpdateString("name","Workshop announcements");
    body.UpdateString("description","People who requested workshop updates");
    success = http.HttpJson("POST","https://api.cc.email/v3/contact_lists",body,"application/json",resp);
    if (resp.get_StatusCode() == 409) {
        std::cout << "A conflicting list already exists. Check the existing lists before retrying." << "\r\n";
        return;
    }

    if ((success != true) || (resp.get_StatusCode() != 201)) {
        std::cout << "Expected HTTP 201. Received " << resp.get_StatusCode() << "\r\n";
        if (resp.get_StatusCode() == 401) {
            std::cout << "Refresh the saved access token or authorize again." << "\r\n";
        }

        return;
    }

    CkJsonObject created;
    success = created.Load(resp.bodyStr());
    if (success != true) {
        std::cout << "Expected a JSON response." << "\r\n";
        return;
    }

    if ((created.JsonTypeOf("list_id") != 1) || (created.StringOfEquals("list_id","",true) == true)) {
        std::cout << "Missing list_id in the response." << "\r\n";
        return;
    }

    std::cout << "Created " << created.stringOf("name") << ". List ID: " << created.stringOf("list_id") << "\r\n";
    }