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

Constant Contact V3: Find a contact by email address

See more Constant Contact Examples

Look up an exact email address across contact states, including deleted contacts. Pass the address as a query parameter so a literal plus sign survives URL encoding. Read the stored contact ID, email permission, and deletion marker. An empty contacts array means no match. 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 oQuery
LOCAL oReply
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")

oQuery := CreateObject("Chilkat.HttpRequest")
oQuery:HttpVerb := "GET"
oQuery:AddParam("email", "reader+newsletter@example.com")
oQuery:AddParam("status", "all")
nSuccess := oHttp:HttpReq("https://api.cc.email/v3/contacts", oQuery, 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()
    oQuery:destroy()
    RETURN
ENDIF

oReply := CreateObject("Chilkat.JsonObject")
nSuccess := oReply:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Expected a JSON response."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oResp:destroy()
    oQuery:destroy()
    oReply:destroy()
    RETURN
ENDIF

nCount := oReply:SizeOfArray("contacts")
IF (nCount < 0)
    ? "The reply contained no contacts array."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oResp:destroy()
    oQuery:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (nCount == 0)
    ? "No contact has that email address."
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oHttp:destroy()
    oResp:destroy()
    oQuery:destroy()
    oReply:destroy()
    RETURN
ENDIF

i := 0
DO WHILE (i < nCount)
    oReply:I := i
    ? "Contact ID: " + oReply:StringOf("contacts[i].contact_id")
    ? "Email permission: " + oReply:StringOf("contacts[i].email_address.permission_to_send")
    IF (oReply:JsonTypeOf("contacts[i].deleted_at") == 1)
        ? "Deleted at: " + oReply:StringOf("contacts[i].deleted_at")
    ENDIF

    i := i + 1
ENDDO

oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oResp:destroy()
oQuery:destroy()
oReply:destroy()