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

Constant Contact V3: Create a contact

See more Constant Contact Examples

Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. 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_Created

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_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")

loo_Body.UpdateString("create_source","Account")
loo_Body.UpdateString("first_name","Alex")
loo_Body.UpdateString("last_name","Rivera")
loo_Body.UpdateString("email_address.address","reader+newsletter@example.com")
//  This sample assumes the account has recorded a qualifying existing relationship.
loo_Body.UpdateString("email_address.permission_to_send","implicit")
loo_Body.UpdateString("street_addresses[0].kind","work")
loo_Body.UpdateString("street_addresses[0].street","100 Main Street")
loo_Body.UpdateString("street_addresses[0].city","Boston")
loo_Body.UpdateString("street_addresses[0].state","MA")
loo_Body.UpdateString("street_addresses[0].country","United States")
li_Success = loo_Http.HttpJson("POST","https://api.cc.email/v3/contacts",loo_Body,"application/json",loo_Resp)
if loo_Resp.StatusCode = 409 then
    Write-Debug "Contact already exists. Look up the email address to obtain its ID."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Body
    return
end if

if (li_Success <> 1) OR (loo_Resp.StatusCode <> 201) then
    Write-Debug "Expected HTTP 201. 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_Body
    return
end if

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

li_Success = loo_Created.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_Created
    return
end if

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

Write-Debug "Created contact ID: " + loo_Created.StringOf("contact_id")


destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Body
destroy loo_Created