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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoVault
    Variant vEntry
    Handle hoEntry
    Variant vTokens
    Handle hoTokens
    Boolean iLoaded
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoUrl
    Handle hoPage
    Integer iTotal
    Integer iCount
    Integer i
    String sTemp1
    String sTemp2
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Get Create (RefClass(cComChilkatSecrets)) To hoVault
    If (Not(IsComObjectCreated(hoVault))) Begin
        Send CreateComObject of hoVault
    End
    Set ComLocation Of hoVault To "local_manager"
    Set ComAutoSplit Of hoVault To True
    Get Create (RefClass(cComChilkatJsonObject)) To hoEntry
    If (Not(IsComObjectCreated(hoEntry))) Begin
        Send CreateComObject of hoEntry
    End
    Get ComUpdateString Of hoEntry "appName" "ConstantContactV3" To iSuccess
    Get ComUpdateString Of hoEntry "service" "OAuth2" To iSuccess
    Get ComUpdateString Of hoEntry "username" "my-account" To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
    If (Not(IsComObjectCreated(hoTokens))) Begin
        Send CreateComObject of hoTokens
    End
    Get pvComObject of hoEntry to vEntry
    Get pvComObject of hoTokens to vTokens
    Get ComGetSecretJson Of hoVault vEntry vTokens To iLoaded
    If (iLoaded <> True) Begin
        Showln "Run the desktop authorization example using this account and store."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoTokens "access_token" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "The saved authorization has no access token."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Get ComStringOf Of hoTokens "access_token" To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End

    Get ComSetUrlVar Of hoHttp "list" "LIST_ID" To iSuccess
    Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
    If (Not(IsComObjectCreated(hoUrl))) Begin
        Send CreateComObject of hoUrl
    End
    Get ComAppend Of hoUrl "https://api.cc.email/v3/contacts?lists={$list}&limit=50" To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoPage
    If (Not(IsComObjectCreated(hoPage))) Begin
        Send CreateComObject of hoPage
    End
    Move 0 To iTotal
    While ((ComLength(hoUrl)) > 0)
        Get ComGetAsString Of hoUrl To sTemp1
        Get pvComObject of hoResp to vResp
        Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
        Get ComStatusCode Of hoResp To iTemp1
        If ((iSuccess <> True) Or (iTemp1 <> 200)) Begin
            Get ComStatusCode Of hoResp To iTemp1
            Showln "Expected HTTP 200. Received " iTemp1
            Get ComStatusCode Of hoResp To iTemp1
            If (iTemp1 = 401) Begin
                Showln "Refresh the saved access token or authorize again."
            End

            Procedure_Return
        End

        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoPage sTemp1 To iSuccess
        Get ComSizeOfArray Of hoPage "contacts" To iCount
        If ((iSuccess <> True) Or (iCount < 0)) Begin
            Showln "Expected a page of contacts."
            Procedure_Return
        End

        Move 0 To i
        While (i < iCount)
            Set ComI Of hoPage To i
            Get ComStringOf Of hoPage "contacts[i].contact_id" To sTemp1
            Get ComStringOf Of hoPage "contacts[i].email_address.address" To sTemp2
            Showln sTemp1 "  " sTemp2
            Move (i + 1) To i
        Loop

        Move (iTotal + iCount) To iTotal
        Send ComClear To hoUrl
        Get ComJsonTypeOf Of hoPage "_links.next.href" To iTemp1
        If (iTemp1 = 1) Begin
            Get ComStringOf Of hoPage "_links.next.href" To sTemp1
            Get ComAppend Of hoUrl sTemp1 To iSuccess
            //  The documented cursor link is relative to api.cc.email.
            Get ComStartsWith Of hoUrl "/v3/contacts?" True To bTemp1
            If (bTemp1 <> True) Begin
                Showln "Unexpected pagination link. Stopped before following it."
                Procedure_Return
            End

            Get ComPrepend Of hoUrl "https://api.cc.email" To iSuccess
        End

    Loop

    Showln "List members read: " iTotal


End_Procedure