Sample code for 30+ languages & platforms
DataFlex 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 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
    Variant vQuery
    Handle hoQuery
    Handle hoReply
    Integer iCount
    Integer i
    String sTemp1
    Integer iTemp1

    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 Create (RefClass(cComChilkatHttpRequest)) To hoQuery
    If (Not(IsComObjectCreated(hoQuery))) Begin
        Send CreateComObject of hoQuery
    End
    Set ComHttpVerb Of hoQuery To "GET"
    Send ComAddParam To hoQuery "email" "reader+newsletter@example.com"
    Send ComAddParam To hoQuery "status" "all"
    Get pvComObject of hoQuery to vQuery
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp "https://api.cc.email/v3/contacts" vQuery 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 Create (RefClass(cComChilkatJsonObject)) To hoReply
    If (Not(IsComObjectCreated(hoReply))) Begin
        Send CreateComObject of hoReply
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoReply sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a JSON response."
        Procedure_Return
    End

    Get ComSizeOfArray Of hoReply "contacts" To iCount
    If (iCount < 0) Begin
        Showln "The reply contained no contacts array."
        Procedure_Return
    End

    If (iCount = 0) Begin
        Showln "No contact has that email address."
        Procedure_Return
    End

    Move 0 To i
    While (i < iCount)
        Set ComI Of hoReply To i
        Get ComStringOf Of hoReply "contacts[i].contact_id" To sTemp1
        Showln "Contact ID: " sTemp1
        Get ComStringOf Of hoReply "contacts[i].email_address.permission_to_send" To sTemp1
        Showln "Email permission: " sTemp1
        Get ComJsonTypeOf Of hoReply "contacts[i].deleted_at" To iTemp1
        If (iTemp1 = 1) Begin
            Get ComStringOf Of hoReply "contacts[i].deleted_at" To sTemp1
            Showln "Deleted at: " sTemp1
        End

        Move (i + 1) To i
    Loop



End_Procedure