Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local oauth = chilkat.OAuth2.new()
oauth.authorization_endpoint = "https://authz.constantcontact.com/oauth2/default/v1/authorize"
oauth.token_endpoint = "https://authz.constantcontact.com/oauth2/default/v1/token"
oauth.client_id = "PKCE_CLIENT_ID"
oauth.local_host = "localhost"
oauth.listen_port = 3017
-- Run authorization again if the account previously granted only contact access.
oauth.scope = "contact_data campaign_data offline_access"
oauth.code_challenge = true
oauth.code_challenge_method = "S256"
oauth.use_basic_auth = false
local url = oauth:start_auth()
if not oauth.last_method_success then
    print("Could not start the local listener. Check the registered port.")
    return
end

local opened = oauth:launch_browser(url)
if not opened then
    oauth:cancel()
    print("Could not open the system browser.")
    return
end

local waited = 0
while (waited < 300000) and (oauth.auth_flow_state < 3) do
    oauth:sleep_ms(100)
    waited = waited + 100
end

if oauth.auth_flow_state < 3 then
    oauth:cancel()
    print("Authorization timed out.")
    return
end

if oauth.auth_flow_state == 4 then
    print("Authorization was declined.")
    return
end

if oauth.auth_flow_state ~= 3 then
    print("Authorization failed. Check the PKCE app registration and exact redirect URI.")
    return
end

local reply = chilkat.JsonObject.new()
local parsed = reply:load(oauth.access_token_response)
if (reply:json_type_of("access_token") ~= 1) or (reply:int_of("expires_in") <= 0) then
    print("Token response is missing an access token or lifetime.")
    return
end

if reply:string_of_equals("access_token", "", true) then
    print("The returned access token is empty.")
    return
end

if (reply:json_type_of("refresh_token") ~= 1) or (reply:string_of_equals("refresh_token", "", true)) then
    print("Missing refresh token. Check the offline_access scope and authorize again.")
    return
end

local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "ConstantContactV3")
entry:update_string("service", "OAuth2")
entry:update_string("username", "my-account")
local tokens = chilkat.JsonObject.new()
tokens:update_string("client_id", oauth.client_id)
tokens:update_string("redirect_uri", "http://localhost:3017/")
tokens:update_string("refresh_token", reply:string_of("refresh_token"))
local now = chilkat.DateTime.new()
now:set_from_current_system_time()
tokens:update_string("obtained_at_utc", now:get_as_timestamp(false))
tokens:update_string("access_token", reply:string_of("access_token"))
tokens:update_int("expires_in", reply:int_of("expires_in"))
local saved = vault:update_secret_json(entry, tokens)
if not saved then
    print("Could not save the current token record. Check the credential store.")
    return
end

print("Authorization saved. Access-token lifetime in seconds: " .. tokens:int_of("expires_in"))