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

Constant Contact V3: Create a contact

See more Constant Contact Examples

Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. 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>

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;

    CkJsonObjectW body;
    body.UpdateString(L"create_source",L"Account");
    body.UpdateString(L"first_name",L"Alex");
    body.UpdateString(L"last_name",L"Rivera");
    body.UpdateString(L"email_address.address",L"reader+newsletter@example.com");
    //  This sample assumes the account has recorded a qualifying existing relationship.
    body.UpdateString(L"email_address.permission_to_send",L"implicit");
    body.UpdateString(L"street_addresses[0].kind",L"work");
    body.UpdateString(L"street_addresses[0].street",L"100 Main Street");
    body.UpdateString(L"street_addresses[0].city",L"Boston");
    body.UpdateString(L"street_addresses[0].state",L"MA");
    body.UpdateString(L"street_addresses[0].country",L"United States");
    success = http.HttpJson(L"POST",L"https://api.cc.email/v3/contacts",body,L"application/json",resp);
    if (resp.get_StatusCode() == 409) {
        wprintf(L"Contact already exists. Look up the email address to obtain its ID.\n");
        return;
    }

    if ((success != true) || (resp.get_StatusCode() != 201)) {
        wprintf(L"Expected HTTP 201. Received %d\n",resp.get_StatusCode());
        if (resp.get_StatusCode() == 401) {
            wprintf(L"Refresh the saved access token or authorize again.\n");
        }

        return;
    }

    CkJsonObjectW created;
    success = created.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Expected a JSON response.\n");
        return;
    }

    if ((created.JsonTypeOf(L"contact_id") != 1) || (created.StringOfEquals(L"contact_id",L"",true) == true)) {
        wprintf(L"Missing contact_id in the response.\n");
        return;
    }

    wprintf(L"Created contact ID: %s\n",created.stringOf(L"contact_id"));
    }