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

Constant Contact V3: Authorize a desktop app with PKCE

See more Constant Contact Examples

Register a V3 app for the public-client PKCE flow and register http://localhost:3017/ exactly, including the trailing slash. Chilkat opens the system browser, receives the redirect on the same computer, and exchanges the code using the PKCE verifier. Request contact_data for contacts and lists, campaign_data for email campaigns and reporting, and offline_access for refresh. If the app already has a contacts-only grant, repeat this browser authorization to grant campaign access. Refreshing the old token cannot add a scope. Use a client ID from the PKCE app registration.

The examples share a Chilkat Secrets record. Windows Credential Manager and Apple Keychain persist the local record. On systems where the local manager uses process memory, configure a persistent Secrets backend for all examples. Give each authorized account its own record name. If migrating from the old token-file example, authorize once to populate the store.

Constant Contact developer guide

Chilkat Xbase++ Downloads

Xbase++
LOCAL oOauth
LOCAL cUrl
LOCAL nOpened
LOCAL nWaited
LOCAL oReply
LOCAL nParsed
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL oNow
LOCAL nSaved

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
oOauth := CreateObject("Chilkat.OAuth2")
oOauth:AuthorizationEndpoint := "https://authz.constantcontact.com/oauth2/default/v1/authorize"
oOauth:TokenEndpoint := "https://authz.constantcontact.com/oauth2/default/v1/token"
oOauth:ClientId := "PKCE_CLIENT_ID"
oOauth:LocalHost := "localhost"
oOauth:ListenPort := 3017
//  Run authorization again if the account previously granted only contact access.
oOauth:Scope := "contact_data campaign_data offline_access"
oOauth:CodeChallenge := 1
oOauth:CodeChallengeMethod := "S256"
oOauth:UseBasicAuth := 0
cUrl := oOauth:StartAuth()
IF (oOauth:LastMethodSuccess != 1)
    ? "Could not start the local listener. Check the registered port."
    oOauth:destroy()
    RETURN
ENDIF

nOpened := oOauth:LaunchBrowser(cUrl)
IF (nOpened != 1)
    oOauth:Cancel()
    ? "Could not open the system browser."
    oOauth:destroy()
    RETURN
ENDIF

nWaited := 0
DO WHILE ((nWaited < 300000) .AND. (oOauth:AuthFlowState < 3))
    oOauth:SleepMs(100)
    nWaited := nWaited + 100
ENDDO
IF (oOauth:AuthFlowState < 3)
    oOauth:Cancel()
    ? "Authorization timed out."
    oOauth:destroy()
    RETURN
ENDIF

IF (oOauth:AuthFlowState == 4)
    ? "Authorization was declined."
    oOauth:destroy()
    RETURN
ENDIF

IF (oOauth:AuthFlowState != 3)
    ? "Authorization failed. Check the PKCE app registration and exact redirect URI."
    oOauth:destroy()
    RETURN
ENDIF

oReply := CreateObject("Chilkat.JsonObject")
nParsed := oReply:Load(oOauth:AccessTokenResponse)
IF ((oReply:JsonTypeOf("access_token") != 1) .OR. (oReply:IntOf("expires_in") <= 0))
    ? "Token response is missing an access token or lifetime."
    oOauth:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:StringOfEquals("access_token", "", 1) == 1)
    ? "The returned access token is empty."
    oOauth:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF ((oReply:JsonTypeOf("refresh_token") != 1) .OR. (oReply:StringOfEquals("refresh_token", "", 1) == 1))
    ? "Missing refresh token. Check the offline_access scope and authorize again."
    oOauth:destroy()
    oReply:destroy()
    RETURN
ENDIF

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")
oTokens:UpdateString("client_id", oOauth:ClientId)
oTokens:UpdateString("redirect_uri", "http://localhost:3017/")
oTokens:UpdateString("refresh_token", oReply:StringOf("refresh_token"))
oNow := CreateObject("Chilkat.CkDateTime")
oNow:SetFromCurrentSystemTime()
oTokens:UpdateString("obtained_at_utc", oNow:GetAsTimestamp(0))
oTokens:UpdateString("access_token", oReply:StringOf("access_token"))
oTokens:UpdateInt("expires_in", oReply:IntOf("expires_in"))
nSaved := oVault:UpdateSecretJson(oEntry, oTokens)
IF (nSaved != 1)
    ? "Could not save the current token record. Check the credential store."
    oOauth:destroy()
    oReply:destroy()
    oVault:destroy()
    oEntry:destroy()
    oTokens:destroy()
    oNow:destroy()
    RETURN
ENDIF

? "Authorization saved. Access-token lifetime in seconds: " + Str(oTokens:IntOf("expires_in"))

oOauth:destroy()
oReply:destroy()
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oNow:destroy()