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

Xojo Plugin
Dim success As Boolean
success = False

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

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

Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.AuthToken = tokens.StringOf("access_token")
Dim resp As New Chilkat.HttpResponse

Dim query As New Chilkat.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)) Then
    System.DebugLog("Expected HTTP 200. Received " + Str(resp.StatusCode))
    If (resp.StatusCode = 401) Then
        System.DebugLog("Refresh the saved access token or authorize again.")
    End If

    Return
End If

Dim matches As New Chilkat.JsonObject
success = matches.Load(resp.BodyStr)
If (success <> True) Then
    System.DebugLog("Expected a JSON response.")
    Return
End If

If ((matches.SizeOfArray("contacts") <> 1) Or (matches.JsonTypeOf("contacts[0].deleted_at") <> 1)) Then
    System.DebugLog("Expected one deleted contact for this email. No restoration submitted.")
    Return
End If

If ((matches.JsonTypeOf("contacts[0].contact_id") <> 1) Or (matches.StringOfEquals("contacts[0].contact_id","",True) = True)) Then
    System.DebugLog("Missing contacts[0].contact_id in the response.")
    Return
End If

Dim body As New Chilkat.JsonObject
success = body.UpdateString("source.contact_ids[0]",matches.StringOf("contacts[0].contact_id"))
success = 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)) Then
    System.DebugLog("Could not start the membership activity. HTTP " + Str(resp.StatusCode))
    Return
End If

Dim job As New Chilkat.JsonObject
success = job.Load(resp.BodyStr)
If (success <> True) Then
    System.DebugLog("Expected a JSON response.")
    Return
End If

If ((job.JsonTypeOf("activity_id") <> 1) Or (job.StringOfEquals("activity_id","",True) = True)) Then
    System.DebugLog("Missing activity_id in the response.")
    Return
End If

Dim activityId As String
activityId = job.StringOf("activity_id")
System.DebugLog("Submitted activity ID: " + activityId)
success = http.SetUrlVar("job",activityId)
//  Poll briefly. A longer-running job can be checked later using the same ID.
Dim attempt As Int32
attempt = 0
While (attempt < 5)
    success = http.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",resp)
    If ((success <> True) Or (resp.StatusCode <> 200)) Then
        System.DebugLog("Status check returned HTTP " + Str(resp.StatusCode) + ". Keep the activity ID and check again later.")
        Return
    End If

    success = job.Load(resp.BodyStr)
    If ((success <> True) Or (job.JsonTypeOf("state") <> 1)) Then
        System.DebugLog("Missing activity state. Keep the activity ID.")
        Return
    End If

    Dim state As String
    state = job.StringOf("state")
    If ((state <> "initialized") And (state <> "processing")) Then
        System.DebugLog("Activity state: " + state)
        Dim i As Int32
        i = 0
        While (i < job.SizeOfArray("activity_errors"))
            job.I = i
            System.DebugLog(job.StringOf("activity_errors[i]"))
            i = i + 1
        Wend
        Return
    End If

    attempt = attempt + 1
    If (attempt < 5) Then
        http.SleepMs 2000
    End If

Wend
System.DebugLog("Still processing. Recheck the activity ID before submitting another request.")