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

Go
    success := false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    vault := chilkat.NewSecrets()
    vault.SetLocation("local_manager")
    vault.SetAutoSplit(true)
    entry := chilkat.NewJsonObject()
    entry.UpdateString("appName","ConstantContactV3")
    entry.UpdateString("service","OAuth2")
    entry.UpdateString("username","my-account")
    tokens := chilkat.NewJsonObject()
    loaded := vault.GetSecretJson(entry,tokens)
    if loaded != true {
        fmt.Println("Run the desktop authorization example using this account and store.")
        vault.DisposeSecrets()
        entry.DisposeJsonObject()
        tokens.DisposeJsonObject()
        return
    }

    if tokens.JsonTypeOf("access_token") != 1 {
        fmt.Println("The saved authorization has no access token.")
        vault.DisposeSecrets()
        entry.DisposeJsonObject()
        tokens.DisposeJsonObject()
        return
    }

    http := chilkat.NewHttp()
    http.SetRequireSslCertVerify(true)
    http.SetAuthToken(tokens.StringOf("access_token"))
    resp := chilkat.NewHttpResponse()

    http.SetUrlVar("list","LIST_ID")
    url := chilkat.NewStringBuilder()
    url.Append("https://api.cc.email/v3/contacts?lists={$list}&limit=50")
    page := chilkat.NewJsonObject()
    total := 0
    for (url.Length() > 0) {
        success = http.HttpNoBody("GET",*url.GetAsString(),resp)
        if (success != true) || (resp.StatusCode() != 200) {
            fmt.Println("Expected HTTP 200. Received ", resp.StatusCode())
            if resp.StatusCode() == 401 {
                fmt.Println("Refresh the saved access token or authorize again.")
            }

            vault.DisposeSecrets()
            entry.DisposeJsonObject()
            tokens.DisposeJsonObject()
            http.DisposeHttp()
            resp.DisposeHttpResponse()
            url.DisposeStringBuilder()
            page.DisposeJsonObject()
            return
        }

        success = page.Load(resp.BodyStr())
        count := page.SizeOfArray("contacts")
        if (success != true) || (count < 0) {
            fmt.Println("Expected a page of contacts.")
            vault.DisposeSecrets()
            entry.DisposeJsonObject()
            tokens.DisposeJsonObject()
            http.DisposeHttp()
            resp.DisposeHttpResponse()
            url.DisposeStringBuilder()
            page.DisposeJsonObject()
            return
        }

        i := 0
        for (i < count) {
            page.SetI(i)
            fmt.Println(*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") == 1 {
            url.Append(*page.StringOf("_links.next.href"))
            //  The documented cursor link is relative to api.cc.email.
            if url.StartsWith("/v3/contacts?",true) != true {
                fmt.Println("Unexpected pagination link. Stopped before following it.")
                vault.DisposeSecrets()
                entry.DisposeJsonObject()
                tokens.DisposeJsonObject()
                http.DisposeHttp()
                resp.DisposeHttpResponse()
                url.DisposeStringBuilder()
                page.DisposeJsonObject()
                return
            }

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

    }

    fmt.Println("List members read: ", total)

    vault.DisposeSecrets()
    entry.DisposeJsonObject()
    tokens.DisposeJsonObject()
    http.DisposeHttp()
    resp.DisposeHttpResponse()
    url.DisposeStringBuilder()
    page.DisposeJsonObject()