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

B4X
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim vault As ChilkatSecrets
vault.Initialize("vault")
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As ChilkatJsonObject
entry.Initialize
entry.UpdateString("appName", "ConstantContactV3")
entry.UpdateString("service", "OAuth2")
entry.UpdateString("username", "my-account")
Dim tokens As ChilkatJsonObject
tokens.Initialize
Dim loaded As Boolean = vault.GetSecretJson(entry, tokens)
If loaded <> True Then
    Log("Run the desktop authorization example using this account and store.")
    Return
End If

If tokens.JsonTypeOf("access_token") <> 1 Then
    Log("The saved authorization has no access token.")
    Return
End If

Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.AuthToken = tokens.StringOf("access_token")
Dim resp As ChilkatHttpResponse
resp.Initialize

Dim campaignName As String = "Workshop newsletter"
Dim campaignId As String = ""
Dim url As ChilkatStringBuilder
url.Initialize
url.Append("https://api.cc.email/v3/emails?limit=50")
Dim page As ChilkatJsonObject
page.Initialize
Do While ((url.Length > 0) And (campaignId = ""))
    success = http.HttpNoBody("GET", url.GetAsString, resp)
    If (success <> True) And (resp.StatusCode <= 0) Then
        Log("Read campaigns: HTTP request failed.")
        Return
    End If

    If (success <> True) Or (resp.StatusCode <> 200) Then
        Log("Read campaigns: HTTP " & resp.StatusCode & " " & resp.BodyStr)
        Return
    End If

    success = page.Load(resp.BodyStr)
    Dim count As Int = page.SizeOfArray("campaigns")
    If (success <> True) Or (count < 0) Then
        Log("Expected a page of campaigns.")
        Return
    End If

    Dim i As Int = 0
    Do While ((i < count) And (campaignId = ""))
        page.I = i
        If page.StringOfEquals("campaigns[i].name", campaignName, True) = True Then
            campaignId = page.StringOf("campaigns[i].campaign_id")
        End If

        i = i + 1
    Loop
    url.Clear
    If (campaignId = "") And (page.JsonTypeOf("_links.next.href") = 1) Then
        url.Append(page.StringOf("_links.next.href"))
        If url.StartsWith("/v3/emails?", True) <> True Then
            Log("Unexpected campaign pagination link.")
            Return
        End If

        url.Prepend("https://api.cc.email")
    End If

Loop
If campaignId = "" Then
    Log("No campaign named " & campaignName)
    Return
End If

http.SetUrlVar("campaign", campaignId)
success = http.HttpNoBody("GET", "https://api.cc.email/v3/emails/{$campaign}", resp)
If (success <> True) And (resp.StatusCode <= 0) Then
    Log("Read campaign: HTTP request failed.")
    Return
End If

If (success <> True) Or (resp.StatusCode <> 200) Then
    Log("Read campaign: HTTP " & resp.StatusCode & " " & resp.BodyStr)
    Return
End If

Dim campaign As ChilkatJsonObject
campaign.Initialize
success = campaign.Load(resp.BodyStr)
If success <> True Then
    Log("Expected a JSON object in the response.")
    Return
End If

'  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
Dim primary As ChilkatJsonObject
primary.Initialize
success = campaign.FindRecord2("campaign_activities", "role", "primary_email", True, primary)
If (success <> True) Or (primary.JsonTypeOf("campaign_activity_id") <> 1) Then
    Log("No primary email activity ID in the response.")
    Return
End If

Dim selectedCampaign As String = campaign.StringOf("campaign_id")
Dim selectedActivity As String = primary.StringOf("campaign_activity_id")
If (selectedCampaign = "") Or (selectedActivity = "") Then
    Log("Missing campaign or activity ID.")
    Return
End If

Log("Campaign ID: " & selectedCampaign)
Log("Primary activity ID: " & selectedActivity)
Dim selection As ChilkatJsonObject
selection.Initialize
selection.UpdateString("name", campaign.StringOf("name"))
selection.UpdateString("campaign_id", selectedCampaign)
selection.UpdateString("campaign_activity_id", selectedActivity)
success = selection.WriteFile("newsletter-selection.json")
If success <> True Then
    Log("Could not save newsletter-selection.json. Keep the IDs printed above.")
    Return
End If

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