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

Constant Contact V3: Read every page of list members

See more Constant Contact Examples

Filter the contacts collection with lists and a list ID. To query multiple lists together, supply their comma-separated IDs. Follow the returned cursor links and report the total after the last page. The default contacts query excludes deleted records. A list member can still have unsubscribed email permission. 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_Url
oleobject loo_Page
long li_Total
long li_Count
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_Http.SetUrlVar("list","LIST_ID")
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Url.Append("https://api.cc.email/v3/contacts?lists={$list}&limit=50")
loo_Page = create oleobject
li_rc = loo_Page.ConnectToNewObject("Chilkat.JsonObject")

li_Total = 0
do while (loo_Url.Length > 0)
    li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),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_Url
        destroy loo_Page
        return
    end if

    li_Success = loo_Page.Load(loo_Resp.BodyStr)
    li_Count = loo_Page.SizeOfArray("contacts")
    if (li_Success <> 1) OR (li_Count < 0) then
        Write-Debug "Expected a page of contacts."
        destroy loo_Vault
        destroy loo_Entry
        destroy loo_Tokens
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Url
        destroy loo_Page
        return
    end if

    i = 0
    do while (i < li_Count)
        loo_Page.I = i
        Write-Debug loo_Page.StringOf("contacts[i].contact_id") + "  " + loo_Page.StringOf("contacts[i].email_address.address")
        i = i + 1
    loop
    li_Total = li_Total + li_Count
    loo_Url.Clear()
    if loo_Page.JsonTypeOf("_links.next.href") = 1 then
        loo_Url.Append(loo_Page.StringOf("_links.next.href"))
        //  The documented cursor link is relative to api.cc.email.
        if loo_Url.StartsWith("/v3/contacts?",1) <> 1 then
            Write-Debug "Unexpected pagination link. Stopped before following it."
            destroy loo_Vault
            destroy loo_Entry
            destroy loo_Tokens
            destroy loo_Http
            destroy loo_Resp
            destroy loo_Url
            destroy loo_Page
            return
        end if

        loo_Url.Prepend("https://api.cc.email")
    end if

loop
Write-Debug "List members read: " + string(li_Total)


destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Url
destroy loo_Page