Sample code for 30+ languages & platforms
Python Requires Chilkat v11.0.0+

Constant Contact V3: Find a contact list by name

See more Constant Contact Examples

Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Python Downloads

Python
import sys
import chilkat2

success = False

# Assumes Chilkat is unlocked. See Global Unlock Sample.
vault = chilkat2.Secrets()
vault.Location = "local_manager"
vault.AutoSplit = True
entry = chilkat2.JsonObject()
entry.UpdateString("appName","ConstantContactV3")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","my-account")
tokens = chilkat2.JsonObject()
loaded = vault.GetSecretJson(entry,tokens)
if (loaded != True):
    print("Run the desktop authorization example using this account and store.")
    sys.exit()

if (tokens.JsonTypeOf("access_token") != 1):
    print("The saved authorization has no access token.")
    sys.exit()

http = chilkat2.Http()
http.RequireSslCertVerify = True
http.AuthToken = tokens.StringOf("access_token")
resp = chilkat2.HttpResponse()

# Use the full display name. AddParam encodes spaces and punctuation.
listName = "Workshop announcements"
query = chilkat2.HttpRequest()
query.HttpVerb = "GET"
query.AddParam("name",listName)
query.AddParam("status","active")
query.AddParam("include_membership_count","active")
success = http.HttpReq("https://api.cc.email/v3/contact_lists",query,resp)
if ((success != True) and (resp.StatusCode <= 0)):
    print("Look up list: HTTP request failed.")
    sys.exit()

if ((success != True) or (resp.StatusCode != 200)):
    print("Look up list: HTTP " + str(resp.StatusCode) + " " + resp.BodyStr)
    sys.exit()

matches = chilkat2.JsonObject()
success = matches.Load(resp.BodyStr)
if (success != True):
    print("Expected a JSON object in the response.")
    sys.exit()

if (matches.SizeOfArray("lists") == 0):
    print("No active email list named " + listName)
    sys.exit()

if ((matches.SizeOfArray("lists") != 1) or (matches.StringOfEquals("lists[0].name",listName,True) != True)):
    print("Expected one exact list name.")
    sys.exit()

listId = matches.StringOf("lists[0].list_id")
if (listId == ""):
    print("Missing list ID.")
    sys.exit()

print(listName + " | List ID: " + listId + " | Mailable contacts: " + str(matches.IntOf("lists[0].membership_count")))