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 Swift Downloads
func chilkatTest() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let oauth = CkoOAuth2()!
oauth.authorizationEndpoint = "https://authz.constantcontact.com/oauth2/default/v1/authorize"
oauth.tokenEndpoint = "https://authz.constantcontact.com/oauth2/default/v1/token"
oauth.clientId = "PKCE_CLIENT_ID"
oauth.localHost = "localhost"
oauth.listenPort = 3017
// Run authorization again if the account previously granted only contact access.
oauth.scope = "contact_data campaign_data offline_access"
oauth.codeChallenge = true
oauth.codeChallengeMethod = "S256"
oauth.useBasicAuth = false
var url: String? = oauth.startAuth()
if oauth.lastMethodSuccess != true {
print("Could not start the local listener. Check the registered port.")
return
}
var opened: Bool = oauth.launchBrowser(url: url)
if opened != true {
oauth.cancel()
print("Could not open the system browser.")
return
}
var waited: Int = 0
while ((waited < 300000) && (oauth.authFlowState.intValue < 3)) {
oauth.sleepMs(millisec: 100)
waited = waited + 100
}
if oauth.authFlowState.intValue < 3 {
oauth.cancel()
print("Authorization timed out.")
return
}
if oauth.authFlowState.intValue == 4 {
print("Authorization was declined.")
return
}
if oauth.authFlowState.intValue != 3 {
print("Authorization failed. Check the PKCE app registration and exact redirect URI.")
return
}
let reply = CkoJsonObject()!
var parsed: Bool = reply.load(json: oauth.accessTokenResponse)
if (reply.jsonType(of: "access_token").intValue != 1) || (reply.int(of: "expires_in").intValue <= 0) {
print("Token response is missing an access token or lifetime.")
return
}
if reply.string(ofEquals: "access_token", value: "", caseSensitive: true) == true {
print("The returned access token is empty.")
return
}
if (reply.jsonType(of: "refresh_token").intValue != 1) || (reply.string(ofEquals: "refresh_token", value: "", caseSensitive: true) == true) {
print("Missing refresh token. Check the offline_access scope and authorize again.")
return
}
let vault = CkoSecrets()!
vault.location = "local_manager"
vault.autoSplit = true
let entry = CkoJsonObject()!
entry.updateString(jsonPath: "appName", value: "ConstantContactV3")
entry.updateString(jsonPath: "service", value: "OAuth2")
entry.updateString(jsonPath: "username", value: "my-account")
let tokens = CkoJsonObject()!
tokens.updateString(jsonPath: "client_id", value: oauth.clientId)
tokens.updateString(jsonPath: "redirect_uri", value: "http://localhost:3017/")
tokens.updateString(jsonPath: "refresh_token", value: reply.string(of: "refresh_token"))
let now = CkoDateTime()!
now.setFromCurrentSystemTime()
tokens.updateString(jsonPath: "obtained_at_utc", value: now.get(asTimestamp: false))
tokens.updateString(jsonPath: "access_token", value: reply.string(of: "access_token"))
tokens.updateInt(jsonPath: "expires_in", value: reply.int(of: "expires_in").intValue)
var saved: Bool = vault.updateSecretJson(jsonId: entry, secret: tokens)
if saved != true {
print("Could not save the current token record. Check the credential store.")
return
}
print("Authorization saved. Access-token lifetime in seconds: \(tokens.int(of: "expires_in").intValue)")
}