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 PowerBuilder Downloads
integer li_rc
oleobject loo_Oauth
string ls_Url
long li_Opened
long li_Waited
oleobject loo_Reply
long li_Parsed
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
oleobject loo_Now
long li_Saved
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Oauth = create oleobject
li_rc = loo_Oauth.ConnectToNewObject("Chilkat.OAuth2")
if li_rc < 0 then
destroy loo_Oauth
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Oauth.AuthorizationEndpoint = "https://authz.constantcontact.com/oauth2/default/v1/authorize"
loo_Oauth.TokenEndpoint = "https://authz.constantcontact.com/oauth2/default/v1/token"
loo_Oauth.ClientId = "PKCE_CLIENT_ID"
loo_Oauth.LocalHost = "localhost"
loo_Oauth.ListenPort = 3017
// Run authorization again if the account previously granted only contact access.
loo_Oauth.Scope = "contact_data campaign_data offline_access"
loo_Oauth.CodeChallenge = 1
loo_Oauth.CodeChallengeMethod = "S256"
loo_Oauth.UseBasicAuth = 0
ls_Url = loo_Oauth.StartAuth()
if loo_Oauth.LastMethodSuccess <> 1 then
Write-Debug "Could not start the local listener. Check the registered port."
destroy loo_Oauth
return
end if
li_Opened = loo_Oauth.LaunchBrowser(ls_Url)
if li_Opened <> 1 then
loo_Oauth.Cancel()
Write-Debug "Could not open the system browser."
destroy loo_Oauth
return
end if
li_Waited = 0
do while ((li_Waited < 300000) AND (loo_Oauth.AuthFlowState < 3))
loo_Oauth.SleepMs(100)
li_Waited = li_Waited + 100
loop
if loo_Oauth.AuthFlowState < 3 then
loo_Oauth.Cancel()
Write-Debug "Authorization timed out."
destroy loo_Oauth
return
end if
if loo_Oauth.AuthFlowState = 4 then
Write-Debug "Authorization was declined."
destroy loo_Oauth
return
end if
if loo_Oauth.AuthFlowState <> 3 then
Write-Debug "Authorization failed. Check the PKCE app registration and exact redirect URI."
destroy loo_Oauth
return
end if
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
li_Parsed = loo_Reply.Load(loo_Oauth.AccessTokenResponse)
if (loo_Reply.JsonTypeOf("access_token") <> 1) OR (loo_Reply.IntOf("expires_in") <= 0) then
Write-Debug "Token response is missing an access token or lifetime."
destroy loo_Oauth
destroy loo_Reply
return
end if
if loo_Reply.StringOfEquals("access_token","",1) = 1 then
Write-Debug "The returned access token is empty."
destroy loo_Oauth
destroy loo_Reply
return
end if
if (loo_Reply.JsonTypeOf("refresh_token") <> 1) OR (loo_Reply.StringOfEquals("refresh_token","",1) = 1) then
Write-Debug "Missing refresh token. Check the offline_access scope and authorize again."
destroy loo_Oauth
destroy loo_Reply
return
end if
loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")
loo_Entry.UpdateString("appName","ConstantContactV3")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","my-account")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")
loo_Tokens.UpdateString("client_id",loo_Oauth.ClientId)
loo_Tokens.UpdateString("redirect_uri","http://localhost:3017/")
loo_Tokens.UpdateString("refresh_token",loo_Reply.StringOf("refresh_token"))
loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")
loo_Now.SetFromCurrentSystemTime()
loo_Tokens.UpdateString("obtained_at_utc",loo_Now.GetAsTimestamp(0))
loo_Tokens.UpdateString("access_token",loo_Reply.StringOf("access_token"))
loo_Tokens.UpdateInt("expires_in",loo_Reply.IntOf("expires_in"))
li_Saved = loo_Vault.UpdateSecretJson(loo_Entry,loo_Tokens)
if li_Saved <> 1 then
Write-Debug "Could not save the current token record. Check the credential store."
destroy loo_Oauth
destroy loo_Reply
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Now
return
end if
Write-Debug "Authorization saved. Access-token lifetime in seconds: " + string(loo_Tokens.IntOf("expires_in"))
destroy loo_Oauth
destroy loo_Reply
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Now