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 Visual FoxPro Downloads
LOCAL loOauth
LOCAL lcUrl
LOCAL lnOpened
LOCAL lnWaited
LOCAL loReply
LOCAL lnParsed
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL loNow
LOCAL lnSaved
* Assumes Chilkat is unlocked. See Global Unlock Sample.
loOauth = CreateObject('Chilkat.OAuth2')
loOauth.AuthorizationEndpoint = "https://authz.constantcontact.com/oauth2/default/v1/authorize"
loOauth.TokenEndpoint = "https://authz.constantcontact.com/oauth2/default/v1/token"
loOauth.ClientId = "PKCE_CLIENT_ID"
loOauth.LocalHost = "localhost"
loOauth.ListenPort = 3017
* Run authorization again if the account previously granted only contact access.
loOauth.Scope = "contact_data campaign_data offline_access"
loOauth.CodeChallenge = 1
loOauth.CodeChallengeMethod = "S256"
loOauth.UseBasicAuth = 0
lcUrl = loOauth.StartAuth()
IF (loOauth.LastMethodSuccess <> 1) THEN
? "Could not start the local listener. Check the registered port."
RELEASE loOauth
CANCEL
ENDIF
lnOpened = loOauth.LaunchBrowser(lcUrl)
IF (lnOpened <> 1) THEN
loOauth.Cancel()
? "Could not open the system browser."
RELEASE loOauth
CANCEL
ENDIF
lnWaited = 0
DO WHILE ((lnWaited < 300000) AND (loOauth.AuthFlowState < 3))
loOauth.SleepMs(100)
lnWaited = lnWaited + 100
ENDDO
IF (loOauth.AuthFlowState < 3) THEN
loOauth.Cancel()
? "Authorization timed out."
RELEASE loOauth
CANCEL
ENDIF
IF (loOauth.AuthFlowState = 4) THEN
? "Authorization was declined."
RELEASE loOauth
CANCEL
ENDIF
IF (loOauth.AuthFlowState <> 3) THEN
? "Authorization failed. Check the PKCE app registration and exact redirect URI."
RELEASE loOauth
CANCEL
ENDIF
loReply = CreateObject('Chilkat.JsonObject')
lnParsed = loReply.Load(loOauth.AccessTokenResponse)
IF ((loReply.JsonTypeOf("access_token") <> 1) OR (loReply.IntOf("expires_in") <= 0)) THEN
? "Token response is missing an access token or lifetime."
RELEASE loOauth
RELEASE loReply
CANCEL
ENDIF
IF (loReply.StringOfEquals("access_token","",1) = 1) THEN
? "The returned access token is empty."
RELEASE loOauth
RELEASE loReply
CANCEL
ENDIF
IF ((loReply.JsonTypeOf("refresh_token") <> 1) OR (loReply.StringOfEquals("refresh_token","",1) = 1)) THEN
? "Missing refresh token. Check the offline_access scope and authorize again."
RELEASE loOauth
RELEASE loReply
CANCEL
ENDIF
loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","ConstantContactV3")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","my-account")
loTokens = CreateObject('Chilkat.JsonObject')
loTokens.UpdateString("client_id",loOauth.ClientId)
loTokens.UpdateString("redirect_uri","http://localhost:3017/")
loTokens.UpdateString("refresh_token",loReply.StringOf("refresh_token"))
loNow = CreateObject('Chilkat.CkDateTime')
loNow.SetFromCurrentSystemTime()
loTokens.UpdateString("obtained_at_utc",loNow.GetAsTimestamp(0))
loTokens.UpdateString("access_token",loReply.StringOf("access_token"))
loTokens.UpdateInt("expires_in",loReply.IntOf("expires_in"))
lnSaved = loVault.UpdateSecretJson(loEntry,loTokens)
IF (lnSaved <> 1) THEN
? "Could not save the current token record. Check the credential store."
RELEASE loOauth
RELEASE loReply
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loNow
CANCEL
ENDIF
? "Authorization saved. Access-token lifetime in seconds: " + STR(loTokens.IntOf("expires_in"))
RELEASE loOauth
RELEASE loReply
RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loNow