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

Xbase++
LOCAL nSuccess
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL nLoaded
LOCAL oHttp
LOCAL oResp
LOCAL oUrl
LOCAL oPage
LOCAL nTotal
LOCAL nCount
LOCAL i

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "ConstantContactV3")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "my-account")
oTokens := CreateObject("Chilkat.JsonObject")
nLoaded := oVault:GetSecretJson(oEntry, oTokens)
IF (nLoaded != 1)
    ? "Run the desktop authorization example using this account and store."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

IF (oTokens:JsonTypeOf("access_token") != 1)
    ? "The saved authorization has no access token."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := oTokens:StringOf("access_token")
oResp := CreateObject("Chilkat.HttpResponse")

oHttp:SetUrlVar("list", "LIST_ID")
oUrl := CreateObject("Chilkat.StringBuilder")
oUrl:Append("https://api.cc.email/v3/contacts?lists={$list}&limit=50")
oPage := CreateObject("Chilkat.JsonObject")
nTotal := 0
DO WHILE (oUrl:Length > 0)
    nSuccess := oHttp:HttpNoBody("GET", oUrl:GetAsString(), oResp)
    IF ((nSuccess != 1) .OR. (oResp:StatusCode != 200))
        ? "Expected HTTP 200. Received " + Str(oResp:StatusCode)
        IF (oResp:StatusCode == 401)
            ? "Refresh the saved access token or authorize again."
        ENDIF

        oVault:destroy()
        oEntry:destroy()
        oTokens:destroy()
        oHttp:destroy()
        oResp:destroy()
        oUrl:destroy()
        oPage:destroy()
        RETURN
    ENDIF

    nSuccess := oPage:Load(oResp:BodyStr)
    nCount := oPage:SizeOfArray("contacts")
    IF ((nSuccess != 1) .OR. (nCount < 0))
        ? "Expected a page of contacts."
        oVault:destroy()
        oEntry:destroy()
        oTokens:destroy()
        oHttp:destroy()
        oResp:destroy()
        oUrl:destroy()
        oPage:destroy()
        RETURN
    ENDIF

    i := 0
    DO WHILE (i < nCount)
        oPage:I := i
        ? oPage:StringOf("contacts[i].contact_id") + "  " + oPage:StringOf("contacts[i].email_address.address")
        i := i + 1
    ENDDO
    nTotal := nTotal + nCount
    oUrl:Clear()
    IF (oPage:JsonTypeOf("_links.next.href") == 1)
        oUrl:Append(oPage:StringOf("_links.next.href"))
        //  The documented cursor link is relative to api.cc.email.
        IF (oUrl:StartsWith("/v3/contacts?", 1) != 1)
            ? "Unexpected pagination link. Stopped before following it."
            oVault:destroy()
            oEntry:destroy()
            oTokens:destroy()
            oHttp:destroy()
            oResp:destroy()
            oUrl:destroy()
            oPage:destroy()
            RETURN
        ENDIF

        oUrl:Prepend("https://api.cc.email")
    ENDIF

ENDDO
? "List members read: " + Str(nTotal)

oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oUrl:destroy()
oPage:destroy()