AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oVault = ObjCreate("Chilkat.Secrets")
$oVault.Location = "local_manager"
$oVault.AutoSplit = True
$oEntry = ObjCreate("Chilkat.JsonObject")
$oEntry.UpdateString("appName","ConstantContactV3")
$oEntry.UpdateString("service","OAuth2")
$oEntry.UpdateString("username","my-account")
$oTokens = ObjCreate("Chilkat.JsonObject")
Local $bLoaded = $oVault.GetSecretJson($oEntry,$oTokens)
If ($bLoaded <> True) Then
ConsoleWrite("Run the desktop authorization example using this account and store." & @CRLF)
Exit
EndIf
If ($oTokens.JsonTypeOf("access_token") <> 1) Then
ConsoleWrite("The saved authorization has no access token." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $oTokens.StringOf("access_token")
$oResp = ObjCreate("Chilkat.HttpResponse")
Local $sCampaignName = "Workshop newsletter"
Local $sCampaignId = ""
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("https://api.cc.email/v3/emails?limit=50")
$oPage = ObjCreate("Chilkat.JsonObject")
While (($oUrl.Length > 0) And ($sCampaignId = ""))
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
If (($bSuccess <> True) And ($oResp.StatusCode <= 0)) Then
ConsoleWrite("Read campaigns: HTTP request failed." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
ConsoleWrite("Read campaigns: HTTP " & $oResp.StatusCode & " " & $oResp.BodyStr & @CRLF)
Exit
EndIf
$bSuccess = $oPage.Load($oResp.BodyStr)
Local $iCount = $oPage.SizeOfArray("campaigns")
If (($bSuccess <> True) Or ($iCount < 0)) Then
ConsoleWrite("Expected a page of campaigns." & @CRLF)
Exit
EndIf
Local $i = 0
While (($i < $iCount) And ($sCampaignId = ""))
$oPage.I = $i
If ($oPage.StringOfEquals("campaigns[i].name",$sCampaignName,True) = True) Then
$sCampaignId = $oPage.StringOf("campaigns[i].campaign_id")
EndIf
$i = $i + 1
Wend
$oUrl.Clear
If (($sCampaignId = "") And ($oPage.JsonTypeOf("_links.next.href") = 1)) Then
$oUrl.Append($oPage.StringOf("_links.next.href"))
If ($oUrl.StartsWith("/v3/emails?",True) <> True) Then
ConsoleWrite("Unexpected campaign pagination link." & @CRLF)
Exit
EndIf
$oUrl.Prepend("https://api.cc.email")
EndIf
Wend
If ($sCampaignId = "") Then
ConsoleWrite("No campaign named " & $sCampaignName & @CRLF)
Exit
EndIf
$oHttp.SetUrlVar("campaign",$sCampaignId)
$bSuccess = $oHttp.HttpNoBody("GET","https://api.cc.email/v3/emails/{$campaign}",$oResp)
If (($bSuccess <> True) And ($oResp.StatusCode <= 0)) Then
ConsoleWrite("Read campaign: HTTP request failed." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
ConsoleWrite("Read campaign: HTTP " & $oResp.StatusCode & " " & $oResp.BodyStr & @CRLF)
Exit
EndIf
$oCampaign = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oCampaign.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a JSON object in the response." & @CRLF)
Exit
EndIf
; The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
$oPrimary = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oCampaign.FindRecord2("campaign_activities","role","primary_email",True,$oPrimary)
If (($bSuccess <> True) Or ($oPrimary.JsonTypeOf("campaign_activity_id") <> 1)) Then
ConsoleWrite("No primary email activity ID in the response." & @CRLF)
Exit
EndIf
Local $selectedCampaign = $oCampaign.StringOf("campaign_id")
Local $selectedActivity = $oPrimary.StringOf("campaign_activity_id")
If (($selectedCampaign = "") Or ($selectedActivity = "")) Then
ConsoleWrite("Missing campaign or activity ID." & @CRLF)
Exit
EndIf
ConsoleWrite("Campaign ID: " & $selectedCampaign & @CRLF)
ConsoleWrite("Primary activity ID: " & $selectedActivity & @CRLF)
$oSelection = ObjCreate("Chilkat.JsonObject")
$oSelection.UpdateString("name",$oCampaign.StringOf("name"))
$oSelection.UpdateString("campaign_id",$selectedCampaign)
$oSelection.UpdateString("campaign_activity_id",$selectedActivity)
$bSuccess = $oSelection.WriteFile("newsletter-selection.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not save newsletter-selection.json. Keep the IDs printed above." & @CRLF)
Exit
EndIf
ConsoleWrite("Saved newsletter-selection.json." & @CRLF)