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

Constant Contact V3: Restore a deleted contact to a list

See more Constant Contact Examples

Look up the deleted contact by email, then restore it by adding its existing ID to a chosen list. Constant Contact documents this membership activity as a restoration method. It changes only membership, preserves the saved profile, and makes no email-permission claim. Select the intended destination list explicitly; previous memberships were removed by deletion. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat AutoIt Downloads

AutoIt
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")

$oQuery = ObjCreate("Chilkat.HttpRequest")
$oQuery.HttpVerb = "GET"
$oQuery.AddParam "email","reader+newsletter@example.com"
$oQuery.AddParam "status","deleted"
$bSuccess = $oHttp.HttpReq("https://api.cc.email/v3/contacts",$oQuery,$oResp)
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
    ConsoleWrite("Expected HTTP 200. Received " & $oResp.StatusCode & @CRLF)
    If ($oResp.StatusCode = 401) Then
        ConsoleWrite("Refresh the saved access token or authorize again." & @CRLF)
    EndIf

    Exit
EndIf

$oMatches = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oMatches.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a JSON response." & @CRLF)
    Exit
EndIf

If (($oMatches.SizeOfArray("contacts") <> 1) Or ($oMatches.JsonTypeOf("contacts[0].deleted_at") <> 1)) Then
    ConsoleWrite("Expected one deleted contact for this email. No restoration submitted." & @CRLF)
    Exit
EndIf

If (($oMatches.JsonTypeOf("contacts[0].contact_id") <> 1) Or ($oMatches.StringOfEquals("contacts[0].contact_id","",True) = True)) Then
    ConsoleWrite("Missing contacts[0].contact_id in the response." & @CRLF)
    Exit
EndIf

$oBody = ObjCreate("Chilkat.JsonObject")
$oBody.UpdateString("source.contact_ids[0]",$oMatches.StringOf("contacts[0].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)