Sample code for 30+ languages & platforms
Rust

GeoOp Exchange Refresh Token for New Access Token

See more GeoOp Examples

Demonstrates how to use the /oauth2/token endpoint to exchange it for a new access token once the current access token has expired.

Note: This example requires Chilkat v9.5.0.65 or greater.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// This example also assumes that OAuth2 access and refresh tokens were previously fetched.
// and saved in a JSON file.  

// First get our previously obtained refresh token.
// { .... "refresh_token":"e6dqdG....mzjpT04w==", .... }
let json_token = chilkat::JsonObject::new();
let _ = json_token.load_file("qa_data/tokens/geoop.json").is_ok();

let rest = chilkat::Rest::new();

// Connect to GeoOp...
let b_auto_reconnect = true;
if rest.connect("login.geoop.com", 443, true, b_auto_reconnect).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// Set the X-Version header.
let _ = rest.add_header("X-Version", "1.0");

// Provide the required form params to get the new access token
println!("refresh_token = {}", json_token.string_of("refresh_token").unwrap_or_default());
let _ = rest.add_query_param("refresh_token", &json_token.string_of("refresh_token").unwrap_or_default());
let _ = rest.add_query_param("grant_type", "refresh_token");
let _ = rest.add_query_param("client_id", "GEOOP-CLIENT-ID");
let _ = rest.add_query_param("client_secret", "GEOOP-CLIENT-SECRET");

let Ok(response_body) = rest.full_request_form_url_encoded("POST", "/oauth2/token") else {
    println!("{}", rest.last_error_text());
    return;
};

// If the response status code did not indicate success, then see what happened..
if rest.response_status_code() != 200 {
    println!("Request Header: ");
    println!("{}", rest.last_request_header());
    println!("----");
    println!("Response StatusCode = {}", rest.response_status_code());
    println!("Response StatusLine: {}", rest.response_status_text());
    println!("Response Header:");
    println!("{}", rest.response_header());
    println!("{}", response_body);
    return;
}

let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
let _ = json.load(&response_body);

// Show the full JSON response.  It should contain the new access token...
println!("{}", json.emit().unwrap_or_default());