Sample code for 30+ languages & platforms
Rust 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 Rust Downloads

Rust
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let oauth = chilkat::OAuth2::new();
oauth.set_authorization_endpoint("https://authz.constantcontact.com/oauth2/default/v1/authorize");
oauth.set_token_endpoint("https://authz.constantcontact.com/oauth2/default/v1/token");
oauth.set_client_id("PKCE_CLIENT_ID");
oauth.set_local_host("localhost");
oauth.set_listen_port(3017);
// Run authorization again if the account previously granted only contact access.
oauth.set_scope("contact_data campaign_data offline_access");
oauth.set_code_challenge(true);
oauth.set_code_challenge_method("S256");
oauth.set_use_basic_auth(false);
let Ok(url) = oauth.start_auth() else {
    println!("Could not start the local listener. Check the registered port.");
    return;
};

if oauth.launch_browser(&url).is_err() {
    let _ = oauth.cancel();
    println!("Could not open the system browser.");
    return;
}

let mut waited = 0;
while (waited < 300000) && (oauth.auth_flow_state() < 3) {
    oauth.sleep_ms(100);
    waited = waited + 100;
}

if oauth.auth_flow_state() < 3 {
    let _ = oauth.cancel();
    println!("Authorization timed out.");
    return;
}

if oauth.auth_flow_state() == 4 {
    println!("Authorization was declined.");
    return;
}

if oauth.auth_flow_state() != 3 {
    println!("Authorization failed. Check the PKCE app registration and exact redirect URI.");
    return;
}

let reply = chilkat::JsonObject::new();
let parsed = reply.load(&oauth.access_token_response()).is_ok();
if (reply.json_type_of("access_token") != 1) || (reply.int_of("expires_in") <= 0) {
    println!("Token response is missing an access token or lifetime.");
    return;
}

if reply.string_of_equals("access_token", "", true) {
    println!("The returned access token is empty.");
    return;
}

if (reply.json_type_of("refresh_token") != 1) || (reply.string_of_equals("refresh_token", "", true)) {
    println!("Missing refresh token. Check the offline_access scope and authorize again.");
    return;
}

let vault = chilkat::Secrets::new();
vault.set_location("local_manager");
vault.set_auto_split(true);
let entry = chilkat::JsonObject::new();
let _ = entry.update_string("appName", "ConstantContactV3");
let _ = entry.update_string("service", "OAuth2");
let _ = entry.update_string("username", "my-account");
let tokens = chilkat::JsonObject::new();
let _ = tokens.update_string("client_id", &oauth.client_id());
let _ = tokens.update_string("redirect_uri", "http://localhost:3017/");
let _ = tokens.update_string("refresh_token", &reply.string_of("refresh_token").unwrap_or_default());
let now = chilkat::DateTime::new();
let _ = now.set_from_current_system_time();
let _ = tokens.update_string("obtained_at_utc", &now.get_as_timestamp(false).unwrap_or_default());
let _ = tokens.update_string("access_token", &reply.string_of("access_token").unwrap_or_default());
let _ = tokens.update_int("expires_in", reply.int_of("expires_in"));
if vault.update_secret_json(&entry, &tokens).is_err() {
    println!("Could not save the current token record. Check the credential store.");
    return;
}

println!("Authorization saved. Access-token lifetime in seconds: {}", tokens.int_of("expires_in"));