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

Visual FoxPro
LOCAL lnSuccess
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lnLoaded
LOCAL loHttp
LOCAL loResp
LOCAL loUrl
LOCAL loPage
LOCAL lnTotal
LOCAL lnCount
LOCAL i

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","ConstantContactV3")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","my-account")
loTokens = CreateObject('Chilkat.JsonObject')
lnLoaded = loVault.GetSecretJson(loEntry,loTokens)
IF (lnLoaded <> 1) THEN
    ? "Run the desktop authorization example using this account and store."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

IF (loTokens.JsonTypeOf("access_token") <> 1) THEN
    ? "The saved authorization has no access token."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.AuthToken = loTokens.StringOf("access_token")
loResp = CreateObject('Chilkat.HttpResponse')

loHttp.SetUrlVar("list","LIST_ID")
loUrl = CreateObject('Chilkat.StringBuilder')
loUrl.Append("https://api.cc.email/v3/contacts?lists={$list}&limit=50")
loPage = CreateObject('Chilkat.JsonObject')
lnTotal = 0
DO WHILE (loUrl.Length > 0)
    lnSuccess = loHttp.HttpNoBody("GET",loUrl.GetAsString(),loResp)
    IF ((lnSuccess <> 1) OR (loResp.StatusCode <> 200)) THEN
        ? "Expected HTTP 200. Received " + STR(loResp.StatusCode)
        IF (loResp.StatusCode = 401) THEN
            ? "Refresh the saved access token or authorize again."
        ENDIF

        RELEASE loVault
        RELEASE loEntry
        RELEASE loTokens
        RELEASE loHttp
        RELEASE loResp
        RELEASE loUrl
        RELEASE loPage
        CANCEL
    ENDIF

    lnSuccess = loPage.Load(loResp.BodyStr)
    lnCount = loPage.SizeOfArray("contacts")
    IF ((lnSuccess <> 1) OR (lnCount < 0)) THEN
        ? "Expected a page of contacts."
        RELEASE loVault
        RELEASE loEntry
        RELEASE loTokens
        RELEASE loHttp
        RELEASE loResp
        RELEASE loUrl
        RELEASE loPage
        CANCEL
    ENDIF

    i = 0
    DO WHILE (i < lnCount)
        loPage.I = i
        ? loPage.StringOf("contacts[i].contact_id") + "  " + loPage.StringOf("contacts[i].email_address.address")
        i = i + 1
    ENDDO
    lnTotal = lnTotal + lnCount
    loUrl.Clear()
    IF (loPage.JsonTypeOf("_links.next.href") = 1) THEN
        loUrl.Append(loPage.StringOf("_links.next.href"))
        *  The documented cursor link is relative to api.cc.email.
        IF (loUrl.StartsWith("/v3/contacts?",1) <> 1) THEN
            ? "Unexpected pagination link. Stopped before following it."
            RELEASE loVault
            RELEASE loEntry
            RELEASE loTokens
            RELEASE loHttp
            RELEASE loResp
            RELEASE loUrl
            RELEASE loPage
            CANCEL
        ENDIF

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

ENDDO
? "List members read: " + STR(lnTotal)

RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loResp
RELEASE loUrl
RELEASE loPage