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

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
$vault = New-Object Chilkat.Secrets
$vault.Location = "local_manager"
$vault.AutoSplit = $true
$entry = New-Object Chilkat.JsonObject
$entry.UpdateString("appName","ConstantContactV3")
$entry.UpdateString("service","OAuth2")
$entry.UpdateString("username","my-account")
$tokens = New-Object Chilkat.JsonObject
$loaded = $vault.GetSecretJson($entry,$tokens)
if ($loaded -ne $true) {
    $("Run the desktop authorization example using this account and store.")
    exit
}

if ($tokens.JsonTypeOf("access_token") -ne 1) {
    $("The saved authorization has no access token.")
    exit
}

$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.AuthToken = $tokens.StringOf("access_token")
$resp = New-Object Chilkat.HttpResponse

$http.SetUrlVar("list","LIST_ID")
$url = New-Object Chilkat.StringBuilder
$url.Append("https://api.cc.email/v3/contacts?lists={$list}&limit=50")
$page = New-Object Chilkat.JsonObject
$total = 0
while (($url.Length -gt 0)) {
    $success = $http.HttpNoBody("GET",$url.GetAsString(),$resp)
    if (($success -ne $true) -or ($resp.StatusCode -ne 200)) {
        $("Expected HTTP 200. Received " + $resp.StatusCode)
        if ($resp.StatusCode -eq 401) {
            $("Refresh the saved access token or authorize again.")
        }

        exit
    }

    $success = $page.Load($resp.BodyStr)
    $count = $page.SizeOfArray("contacts")
    if (($success -ne $true) -or ($count -lt 0)) {
        $("Expected a page of contacts.")
        exit
    }

    $i = 0
    while (($i -lt $count)) {
        $page.I = $i
        $($page.StringOf("contacts[i].contact_id") + "  " + $page.StringOf("contacts[i].email_address.address"))
        $i = $i + 1
    }

    $total = $total + $count
    $url.Clear()
    if ($page.JsonTypeOf("_links.next.href") -eq 1) {
        $url.Append($page.StringOf("_links.next.href"))
        #  The documented cursor link is relative to api.cc.email.
        if ($url.StartsWith("/v3/contacts?",$true) -ne $true) {
            $("Unexpected pagination link. Stopped before following it.")
            exit
        }

        $url.Prepend("https://api.cc.email")
    }

}

$("List members read: " + $total)