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

Python
import sys
import chilkat2

success = False

# Assumes Chilkat is unlocked. See Global Unlock Sample.
vault = chilkat2.Secrets()
vault.Location = "local_manager"
vault.AutoSplit = True
entry = chilkat2.JsonObject()
entry.UpdateString("appName","ConstantContactV3")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","my-account")
tokens = chilkat2.JsonObject()
loaded = vault.GetSecretJson(entry,tokens)
if (loaded != True):
    print("Run the desktop authorization example using this account and store.")
    sys.exit()

if (tokens.JsonTypeOf("access_token") != 1):
    print("The saved authorization has no access token.")
    sys.exit()

http = chilkat2.Http()
http.RequireSslCertVerify = True
http.AuthToken = tokens.StringOf("access_token")
resp = chilkat2.HttpResponse()

query = chilkat2.HttpRequest()
query.HttpVerb = "GET"
query.AddParam("email","reader+newsletter@example.com")
query.AddParam("status","deleted")
success = http.HttpReq("https://api.cc.email/v3/contacts",query,resp)
if ((success != True) or (resp.StatusCode != 200)):
    print("Expected HTTP 200. Received " + str(resp.StatusCode))
    if (resp.StatusCode == 401):
        print("Refresh the saved access token or authorize again.")

    sys.exit()

matches = chilkat2.JsonObject()
success = matches.Load(resp.BodyStr)
if (success != True):
    print("Expected a JSON response.")
    sys.exit()

if ((matches.SizeOfArray("contacts") != 1) or (matches.JsonTypeOf("contacts[0].deleted_at") != 1)):
    print("Expected one deleted contact for this email. No restoration submitted.")
    sys.exit()

if ((matches.JsonTypeOf("contacts[0].contact_id") != 1) or (matches.StringOfEquals("contacts[0].contact_id","",True) == True)):
    print("Missing contacts[0].contact_id in the response.")
    sys.exit()

body = chilkat2.JsonObject()
body.UpdateString("source.contact_ids[0]",matches.StringOf("contacts[0].contact_id"))
body.UpdateString("list_ids[0]","DESTINATION_LIST_ID")
success = http.HttpJson("POST","https://api.cc.email/v3/activities/add_list_memberships",body,"application/json",resp)
if ((success != True) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)):
    print("Could not start the membership activity. HTTP " + str(resp.StatusCode))
    sys.exit()

job = chilkat2.JsonObject()
success = job.Load(resp.BodyStr)
if (success != True):
    print("Expected a JSON response.")
    sys.exit()

if ((job.JsonTypeOf("activity_id") != 1) or (job.StringOfEquals("activity_id","",True) == True)):
    print("Missing activity_id in the response.")
    sys.exit()

activityId = job.StringOf("activity_id")
print("Submitted activity ID: " + activityId)
http.SetUrlVar("job",activityId)
# Poll briefly. A longer-running job can be checked later using the same ID.
attempt = 0
while (attempt < 5) :
    success = http.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",resp)
    if ((success != True) or (resp.StatusCode != 200)):
        print("Status check returned HTTP " + str(resp.StatusCode) + ". Keep the activity ID and check again later.")
        sys.exit()

    success = job.Load(resp.BodyStr)
    if ((success != True) or (job.JsonTypeOf("state") != 1)):
        print("Missing activity state. Keep the activity ID.")
        sys.exit()

    state = job.StringOf("state")
    if ((state != "initialized") and (state != "processing")):
        print("Activity state: " + state)
        i = 0
        while (i < job.SizeOfArray("activity_errors")) :
            job.I = i
            print(job.StringOf("activity_errors[i]"))
            i = i + 1

        sys.exit()

    attempt = attempt + 1
    if (attempt < 5):
        http.SleepMs(2000)

print("Still processing. Recheck the activity ID before submitting another request.")