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 Tcl Downloads
load ./chilkat.dll
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set oauth [new_CkOAuth2]
CkOAuth2_put_AuthorizationEndpoint $oauth "https://authz.constantcontact.com/oauth2/default/v1/authorize"
CkOAuth2_put_TokenEndpoint $oauth "https://authz.constantcontact.com/oauth2/default/v1/token"
CkOAuth2_put_ClientId $oauth "PKCE_CLIENT_ID"
CkOAuth2_put_LocalHost $oauth "localhost"
CkOAuth2_put_ListenPort $oauth 3017
# Run authorization again if the account previously granted only contact access.
CkOAuth2_put_Scope $oauth "contact_data campaign_data offline_access"
CkOAuth2_put_CodeChallenge $oauth 1
CkOAuth2_put_CodeChallengeMethod $oauth "S256"
CkOAuth2_put_UseBasicAuth $oauth 0
set url [CkOAuth2_startAuth $oauth]
if {[CkOAuth2_get_LastMethodSuccess $oauth] != 1} then {
puts "Could not start the local listener. Check the registered port."
delete_CkOAuth2 $oauth
exit
}
set opened [CkOAuth2_LaunchBrowser $oauth $url]
if {$opened != 1} then {
CkOAuth2_Cancel $oauth
puts "Could not open the system browser."
delete_CkOAuth2 $oauth
exit
}
set waited 0
while {expr [$waited < 300000] && [[CkOAuth2_get_AuthFlowState $oauth] < 3]} {
CkOAuth2_SleepMs $oauth 100
set waited [expr $waited + 100]
}
if {[CkOAuth2_get_AuthFlowState $oauth] < 3} then {
CkOAuth2_Cancel $oauth
puts "Authorization timed out."
delete_CkOAuth2 $oauth
exit
}
if {[CkOAuth2_get_AuthFlowState $oauth] == 4} then {
puts "Authorization was declined."
delete_CkOAuth2 $oauth
exit
}
if {[CkOAuth2_get_AuthFlowState $oauth] != 3} then {
puts "Authorization failed. Check the PKCE app registration and exact redirect URI."
delete_CkOAuth2 $oauth
exit
}
set reply [new_CkJsonObject]
set parsed [CkJsonObject_Load $reply [CkOAuth2_accessTokenResponse $oauth]]
if {expr [[CkJsonObject_JsonTypeOf $reply "access_token"] != 1] || [[CkJsonObject_IntOf $reply "expires_in"] <= 0]} then {
puts "Token response is missing an access token or lifetime."
delete_CkOAuth2 $oauth
delete_CkJsonObject $reply
exit
}
if {[CkJsonObject_StringOfEquals $reply "access_token" "" 1] == 1} then {
puts "The returned access token is empty."
delete_CkOAuth2 $oauth
delete_CkJsonObject $reply
exit
}
if {expr [[CkJsonObject_JsonTypeOf $reply "refresh_token"] != 1] || [[CkJsonObject_StringOfEquals $reply "refresh_token" "" 1] == 1]} then {
puts "Missing refresh token. Check the offline_access scope and authorize again."
delete_CkOAuth2 $oauth
delete_CkJsonObject $reply
exit
}
set vault [new_CkSecrets]
CkSecrets_put_Location $vault "local_manager"
CkSecrets_put_AutoSplit $vault 1
set entry [new_CkJsonObject]
CkJsonObject_UpdateString $entry "appName" "ConstantContactV3"
CkJsonObject_UpdateString $entry "service" "OAuth2"
CkJsonObject_UpdateString $entry "username" "my-account"
set tokens [new_CkJsonObject]
CkJsonObject_UpdateString $tokens "client_id" [CkOAuth2_clientId $oauth]
CkJsonObject_UpdateString $tokens "redirect_uri" "http://localhost:3017/"
CkJsonObject_UpdateString $tokens "refresh_token" [CkJsonObject_stringOf $reply "refresh_token"]
set now [new_CkDateTime]
CkDateTime_SetFromCurrentSystemTime $now
CkJsonObject_UpdateString $tokens "obtained_at_utc" [CkDateTime_getAsTimestamp $now 0]
CkJsonObject_UpdateString $tokens "access_token" [CkJsonObject_stringOf $reply "access_token"]
CkJsonObject_UpdateInt $tokens "expires_in" [CkJsonObject_IntOf $reply "expires_in"]
set saved [CkSecrets_UpdateSecretJson $vault $entry $tokens]
if {$saved != 1} then {
puts "Could not save the current token record. Check the credential store."
delete_CkOAuth2 $oauth
delete_CkJsonObject $reply
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkDateTime $now
exit
}
puts "Authorization saved. Access-token lifetime in seconds: [CkJsonObject_IntOf $tokens expires_in]"
delete_CkOAuth2 $oauth
delete_CkJsonObject $reply
delete_CkSecrets $vault
delete_CkJsonObject $entry
delete_CkJsonObject $tokens
delete_CkDateTime $now