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

Constant Contact V3: Find a contact by email address

See more Constant Contact Examples

Look up an exact email address across contact states, including deleted contacts. Pass the address as a query parameter so a literal plus sign survives URL encoding. Read the stored contact ID, email permission, and deletion marker. An empty contacts array means no match. 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 <CkHttpRequest.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;

    CkHttpRequest query;
    query.put_HttpVerb("GET");
    query.AddParam("email","reader+newsletter@example.com");
    query.AddParam("status","all");
    success = http.HttpReq("https://api.cc.email/v3/contacts",query,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;
    }

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

    int count = reply.SizeOfArray("contacts");
    if (count < 0) {
        std::cout << "The reply contained no contacts array." << "\r\n";
        return;
    }

    if (count == 0) {
        std::cout << "No contact has that email address." << "\r\n";
        return;
    }

    int i = 0;
    while ((i < count)) {
        reply.put_I(i);
        std::cout << "Contact ID: " << reply.stringOf("contacts[i].contact_id") << "\r\n";
        std::cout << "Email permission: " << reply.stringOf("contacts[i].email_address.permission_to_send") << "\r\n";
        if (reply.JsonTypeOf("contacts[i].deleted_at") == 1) {
            std::cout << "Deleted at: " << reply.stringOf("contacts[i].deleted_at") << "\r\n";
        }

        i = i + 1;
    }
    }