AutoIt Requires Chilkat v11.0.0+
AutoIt
Constant Contact V3: Add existing contacts to a list
See more Constant Contact Examples
Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. Authorize the app first and use the same 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")
; Use contact IDs from email lookups and the list ID from the selected destination.
$oBody = ObjCreate("Chilkat.JsonObject")
$oBody.UpdateString("source.contact_ids[0]","FIRST_CONTACT_ID")
$oBody.UpdateString("source.contact_ids[1]","SECOND_CONTACT_ID")
$oBody.UpdateString("list_ids[0]","DESTINATION_LIST_ID")
$bSuccess = $oHttp.HttpJson("POST","https://api.cc.email/v3/activities/add_list_memberships",$oBody,"application/json",$oResp)
If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
ConsoleWrite("Could not start the membership activity. HTTP " & $oResp.StatusCode & @CRLF)
Exit
EndIf
$oJob = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJob.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a JSON response." & @CRLF)
Exit
EndIf
If (($oJob.JsonTypeOf("activity_id") <> 1) Or ($oJob.StringOfEquals("activity_id","",True) = True)) Then
ConsoleWrite("Missing activity_id in the response." & @CRLF)
Exit
EndIf
Local $sActivityId = $oJob.StringOf("activity_id")
ConsoleWrite("Submitted activity ID: " & $sActivityId & @CRLF)
$oHttp.SetUrlVar("job",$sActivityId)
; Poll briefly. A longer-running job can be checked later using the same ID.
Local $iAttempt = 0
While ($iAttempt < 5)
$bSuccess = $oHttp.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",$oResp)
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
ConsoleWrite("Status check returned HTTP " & $oResp.StatusCode & ". Keep the activity ID and check again later." & @CRLF)
Exit
EndIf
$bSuccess = $oJob.Load($oResp.BodyStr)
If (($bSuccess <> True) Or ($oJob.JsonTypeOf("state") <> 1)) Then
ConsoleWrite("Missing activity state. Keep the activity ID." & @CRLF)
Exit
EndIf
Local $state = $oJob.StringOf("state")
If (($state <> "initialized") And ($state <> "processing")) Then
ConsoleWrite("Activity state: " & $state & @CRLF)
Local $i = 0
While ($i < $oJob.SizeOfArray("activity_errors"))
$oJob.I = $i
ConsoleWrite($oJob.StringOf("activity_errors[i]") & @CRLF)
$i = $i + 1
Wend
Exit
EndIf
$iAttempt = $iAttempt + 1
If ($iAttempt < 5) Then
$oHttp.SleepMs 2000
EndIf
Wend
ConsoleWrite("Still processing. Recheck the activity ID before submitting another request." & @CRLF)