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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
long li_Loaded
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Query
oleobject loo_Matches
oleobject loo_Body
oleobject loo_Job
string ls_ActivityId
long li_Attempt
string ls_State
long i

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
    destroy loo_Vault
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")

loo_Entry.UpdateString("appName","ConstantContactV3")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","my-account")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")

li_Loaded = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Loaded <> 1 then
    Write-Debug "Run the desktop authorization example using this account and store."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

if loo_Tokens.JsonTypeOf("access_token") <> 1 then
    Write-Debug "The saved authorization has no access token."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Tokens.StringOf("access_token")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Query = create oleobject
li_rc = loo_Query.ConnectToNewObject("Chilkat.HttpRequest")

loo_Query.HttpVerb = "GET"
loo_Query.AddParam("email","reader+newsletter@example.com")
loo_Query.AddParam("status","deleted")
li_Success = loo_Http.HttpReq("https://api.cc.email/v3/contacts",loo_Query,loo_Resp)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
    Write-Debug "Expected HTTP 200. Received " + string(loo_Resp.StatusCode)
    if loo_Resp.StatusCode = 401 then
        Write-Debug "Refresh the saved access token or authorize again."
    end if

    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    return
end if

loo_Matches = create oleobject
li_rc = loo_Matches.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Matches.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Expected a JSON response."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Matches
    return
end if

if (loo_Matches.SizeOfArray("contacts") <> 1) OR (loo_Matches.JsonTypeOf("contacts[0].deleted_at") <> 1) then
    Write-Debug "Expected one deleted contact for this email. No restoration submitted."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Matches
    return
end if

if (loo_Matches.JsonTypeOf("contacts[0].contact_id") <> 1) OR (loo_Matches.StringOfEquals("contacts[0].contact_id","",1) = 1) then
    Write-Debug "Missing contacts[0].contact_id in the response."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Matches
    return
end if

loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")

loo_Body.UpdateString("source.contact_ids[0]",loo_Matches.StringOf("contacts[0].contact_id"))
loo_Body.UpdateString("list_ids[0]","DESTINATION_LIST_ID")
li_Success = loo_Http.HttpJson("POST","https://api.cc.email/v3/activities/add_list_memberships",loo_Body,"application/json",loo_Resp)
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
    Write-Debug "Could not start the membership activity. HTTP " + string(loo_Resp.StatusCode)
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Matches
    destroy loo_Body
    return
end if

loo_Job = create oleobject
li_rc = loo_Job.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Job.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Expected a JSON response."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Matches
    destroy loo_Body
    destroy loo_Job
    return
end if

if (loo_Job.JsonTypeOf("activity_id") <> 1) OR (loo_Job.StringOfEquals("activity_id","",1) = 1) then
    Write-Debug "Missing activity_id in the response."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Query
    destroy loo_Matches
    destroy loo_Body
    destroy loo_Job
    return
end if

ls_ActivityId = loo_Job.StringOf("activity_id")
Write-Debug "Submitted activity ID: " + ls_ActivityId
loo_Http.SetUrlVar("job",ls_ActivityId)
//  Poll briefly. A longer-running job can be checked later using the same ID.
li_Attempt = 0
do while (li_Attempt < 5)
    li_Success = loo_Http.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",loo_Resp)
    if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
        Write-Debug "Status check returned HTTP " + string(loo_Resp.StatusCode) + ". Keep the activity ID and check again later."
        destroy loo_Vault
        destroy loo_Entry
        destroy loo_Tokens
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Query
        destroy loo_Matches
        destroy loo_Body
        destroy loo_Job
        return
    end if

    li_Success = loo_Job.Load(loo_Resp.BodyStr)
    if (li_Success <> 1) OR (loo_Job.JsonTypeOf("state") <> 1) then
        Write-Debug "Missing activity state. Keep the activity ID."
        destroy loo_Vault
        destroy loo_Entry
        destroy loo_Tokens
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Query
        destroy loo_Matches
        destroy loo_Body
        destroy loo_Job
        return
    end if

    ls_State = loo_Job.StringOf("state")
    if (ls_State <> "initialized") AND (ls_State <> "processing") then
        Write-Debug "Activity state: " + ls_State
        i = 0
        do while (i < loo_Job.SizeOfArray("activity_errors"))
            loo_Job.I = i
            Write-Debug loo_Job.StringOf("activity_errors[i]")
            i = i + 1
        loop
        destroy loo_Vault
        destroy loo_Entry
        destroy loo_Tokens
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Query
        destroy loo_Matches
        destroy loo_Body
        destroy loo_Job
        return
    end if

    li_Attempt = li_Attempt + 1
    if li_Attempt < 5 then
        loo_Http.SleepMs(2000)
    end if

loop
Write-Debug "Still processing. Recheck the activity ID before submitting another request."


destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Query
destroy loo_Matches
destroy loo_Body
destroy loo_Job