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

Constant Contact V3: Find a campaign and its primary email activity

See more Constant Contact Examples

Enter the unique campaign name from the account. Follow the campaign collection’s cursor until the name matches, then fetch that campaign’s details. Select the activity whose role is primary_email. A permalink or resend can appear first.

The example writes the two IDs to newsletter-selection.json, the same file produced by the draft example. Run this lookup when selecting an existing newsletter for review or scheduling.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local success = false

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "ConstantContactV3")
entry:update_string("service", "OAuth2")
entry:update_string("username", "my-account")
local tokens = chilkat.JsonObject.new()
local loaded = vault:get_secret_json(entry, tokens)
if not loaded then
    print("Run the desktop authorization example using this account and store.")
    return
end

if tokens:json_type_of("access_token") ~= 1 then
    print("The saved authorization has no access token.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = tokens:string_of("access_token")
local resp = chilkat.HttpResponse.new()

local campaignName = "Workshop newsletter"
local campaignId = ""
local url = chilkat.StringBuilder.new()
url:append("https://api.cc.email/v3/emails?limit=50")
local page = chilkat.JsonObject.new()
while (url.length > 0) and (campaignId == "") do
    success = http:http_no_body("GET", url:get_as_string(), resp)
    if (not success) and (resp.status_code <= 0) then
        print("Read campaigns: HTTP request failed.")
        return
    end

    if (not success) or (resp.status_code ~= 200) then
        print("Read campaigns: HTTP " .. resp.status_code .. " " .. resp.body_str)
        return
    end

    success = page:load(resp.body_str)
    local count = page:size_of_array("campaigns")
    if (not success) or (count < 0) then
        print("Expected a page of campaigns.")
        return
    end

    local i = 0
    while (i < count) and (campaignId == "") do
        page.i = i
        if page:string_of_equals("campaigns[i].name", campaignName, true) then
            campaignId = page:string_of("campaigns[i].campaign_id")
        end

        i = i + 1
    end

    url:clear()
    if (campaignId == "") and (page:json_type_of("_links.next.href") == 1) then
        url:append(page:string_of("_links.next.href"))
        if not url:starts_with("/v3/emails?", true) then
            print("Unexpected campaign pagination link.")
            return
        end

        url:prepend("https://api.cc.email")
    end
end

if campaignId == "" then
    print("No campaign named " .. campaignName)
    return
end

http:set_url_var("campaign", campaignId)
success = http:http_no_body("GET", "https://api.cc.email/v3/emails/{$campaign}", resp)
if (not success) and (resp.status_code <= 0) then
    print("Read campaign: HTTP request failed.")
    return
end

if (not success) or (resp.status_code ~= 200) then
    print("Read campaign: HTTP " .. resp.status_code .. " " .. resp.body_str)
    return
end

local campaign = chilkat.JsonObject.new()
success = campaign:load(resp.body_str)
if not success then
    print("Expected a JSON object in the response.")
    return
end

-- The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
local primary = chilkat.JsonObject.new()
success = campaign:find_record2("campaign_activities", "role", "primary_email", true, primary)
if (not success) or (primary:json_type_of("campaign_activity_id") ~= 1) then
    print("No primary email activity ID in the response.")
    return
end

local selectedCampaign = campaign:string_of("campaign_id")
local selectedActivity = primary:string_of("campaign_activity_id")
if (selectedCampaign == "") or (selectedActivity == "") then
    print("Missing campaign or activity ID.")
    return
end

print("Campaign ID: " .. selectedCampaign)
print("Primary activity ID: " .. selectedActivity)
local selection = chilkat.JsonObject.new()
selection:update_string("name", campaign:string_of("name"))
selection:update_string("campaign_id", selectedCampaign)
selection:update_string("campaign_activity_id", selectedActivity)
success = selection:write_file("newsletter-selection.json")
if not success then
    print("Could not save newsletter-selection.json. Keep the IDs printed above.")
    return
end

print("Saved newsletter-selection.json.")