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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Handle hoOauth
String sUrl
Boolean iOpened
Boolean iSuccess
Integer iWaited
Handle hoReply
Boolean iParsed
Handle hoVault
Variant vEntry
Handle hoEntry
Variant vTokens
Handle hoTokens
Handle hoNow
Boolean iSaved
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatOAuth2)) To hoOauth
If (Not(IsComObjectCreated(hoOauth))) Begin
Send CreateComObject of hoOauth
End
Set ComAuthorizationEndpoint Of hoOauth To "https://authz.constantcontact.com/oauth2/default/v1/authorize"
Set ComTokenEndpoint Of hoOauth To "https://authz.constantcontact.com/oauth2/default/v1/token"
Set ComClientId Of hoOauth To "PKCE_CLIENT_ID"
Set ComLocalHost Of hoOauth To "localhost"
Set ComListenPort Of hoOauth To 3017
// Run authorization again if the account previously granted only contact access.
Set ComScope Of hoOauth To "contact_data campaign_data offline_access"
Set ComCodeChallenge Of hoOauth To True
Set ComCodeChallengeMethod Of hoOauth To "S256"
Set ComUseBasicAuth Of hoOauth To False
Get ComStartAuth Of hoOauth To sUrl
Get ComLastMethodSuccess Of hoOauth To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not start the local listener. Check the registered port."
Procedure_Return
End
Get ComLaunchBrowser Of hoOauth sUrl To iOpened
If (iOpened <> True) Begin
Get ComCancel Of hoOauth To iSuccess
Showln "Could not open the system browser."
Procedure_Return
End
Move 0 To iWaited
While ((iWaited < 300000) And ((ComAuthFlowState(hoOauth)) < 3))
Send ComSleepMs To hoOauth 100
Move (iWaited + 100) To iWaited
Loop
Get ComAuthFlowState Of hoOauth To iTemp1
If (iTemp1 < 3) Begin
Get ComCancel Of hoOauth To iSuccess
Showln "Authorization timed out."
Procedure_Return
End
Get ComAuthFlowState Of hoOauth To iTemp1
If (iTemp1 = 4) Begin
Showln "Authorization was declined."
Procedure_Return
End
Get ComAuthFlowState Of hoOauth To iTemp1
If (iTemp1 <> 3) Begin
Showln "Authorization failed. Check the PKCE app registration and exact redirect URI."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Get ComAccessTokenResponse Of hoOauth To sTemp1
Get ComLoad Of hoReply sTemp1 To iParsed
Get ComJsonTypeOf Of hoReply "access_token" To iTemp1
Get ComIntOf Of hoReply "expires_in" To iTemp2
If ((iTemp1 <> 1) Or (iTemp2 <= 0)) Begin
Showln "Token response is missing an access token or lifetime."
Procedure_Return
End
Get ComStringOfEquals Of hoReply "access_token" "" True To bTemp1
If (bTemp1 = True) Begin
Showln "The returned access token is empty."
Procedure_Return
End
Get ComJsonTypeOf Of hoReply "refresh_token" To iTemp1
Get ComStringOfEquals Of hoReply "refresh_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Missing refresh token. Check the offline_access scope and authorize again."
Procedure_Return
End
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 ComClientId Of hoOauth To sTemp1
Get ComUpdateString Of hoTokens "client_id" sTemp1 To iSuccess
Get ComUpdateString Of hoTokens "redirect_uri" "http://localhost:3017/" To iSuccess
Get ComStringOf Of hoReply "refresh_token" To sTemp1
Get ComUpdateString Of hoTokens "refresh_token" sTemp1 To iSuccess
Get Create (RefClass(cComCkDateTime)) To hoNow
If (Not(IsComObjectCreated(hoNow))) Begin
Send CreateComObject of hoNow
End
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
Get ComGetAsTimestamp Of hoNow False To sTemp1
Get ComUpdateString Of hoTokens "obtained_at_utc" sTemp1 To iSuccess
Get ComStringOf Of hoReply "access_token" To sTemp1
Get ComUpdateString Of hoTokens "access_token" sTemp1 To iSuccess
Get ComIntOf Of hoReply "expires_in" To iTemp1
Get ComUpdateInt Of hoTokens "expires_in" iTemp1 To iSuccess
Get pvComObject of hoEntry to vEntry
Get pvComObject of hoTokens to vTokens
Get ComUpdateSecretJson Of hoVault vEntry vTokens To iSaved
If (iSaved <> True) Begin
Showln "Could not save the current token record. Check the credential store."
Procedure_Return
End
Get ComIntOf Of hoTokens "expires_in" To iTemp1
Showln "Authorization saved. Access-token lifetime in seconds: " iTemp1
End_Procedure