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

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

//  Use contact IDs from email lookups and the list ID from the selected destination.
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")

loo_Body.UpdateString("source.contact_ids[0]","FIRST_CONTACT_ID")
loo_Body.UpdateString("source.contact_ids[1]","SECOND_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_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_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_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_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_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_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_Body
destroy loo_Job