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

Constant Contact V3: Refresh a PKCE access token

See more Constant Contact Examples

Run desktop authorization first. Refresh near the saved expiry or after a 401. Reuse the original client ID and redirect URI and save any rotated refresh token with the new access token. Serialize refreshes for the same account so two processes cannot overwrite the newest grant. If the grant is invalid, authorize again.

Constant Contact developer guide

Chilkat Rust Downloads

Rust
// Assumes Chilkat is unlocked. See Global Unlock Sample.
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();
if vault.get_secret_json(&entry, &tokens).is_err() {
    println!("Run the desktop authorization example using this account and store.");
    return;
}

if (tokens.json_type_of("refresh_token") != 1) || ((tokens.json_type_of("client_id") != 1) || (tokens.json_type_of("redirect_uri") != 1)) {
    println!("The saved PKCE grant is incomplete. Authorize again.");
    return;
}

let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
let form = chilkat::HttpRequest::new();
form.set_http_verb("POST");
form.set_content_type("application/x-www-form-urlencoded");
form.add_header("Accept", "application/json");
form.add_param("grant_type", "refresh_token");
form.add_param("client_id", &tokens.string_of("client_id").unwrap_or_default());
form.add_param("redirect_uri", &tokens.string_of("redirect_uri").unwrap_or_default());
form.add_param("refresh_token", &tokens.string_of("refresh_token").unwrap_or_default());
let resp = chilkat::HttpResponse::new();
let sent = http.http_req("https://authz.constantcontact.com/oauth2/default/v1/token", &form, &resp).is_ok();
let reply = chilkat::JsonObject::new();
let parsed = reply.load(&resp.body_str()).is_ok();
if (!sent) || (resp.status_code() != 200) {
    println!("Token refresh failed. HTTP status {}", resp.status_code());
    if reply.string_of_equals("error", "invalid_grant", true) {
        println!("The refresh grant is no longer valid. Authorize again.");
    }

    return;
}

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 {
    if !reply.string_of_equals("refresh_token", "", true) {
        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"));