Rust
Rust
Get Access Token using a Pre-Created JSON Web Token
See more ABN AMRO Examples
Demonstrates how to get an access token using a pre-created JSON Web Token (JWT).Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// We're going to duplicate this CURL statement:
// curl -X POST https://api-sandbox.abnamro.com/v1/oauth/token \
// -H "Content-Type: application/x-www-form-urlencoded" \
// -H "API-Key: xxxxxx" \
// -d 'client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&grant_type=client_credentials&client_assertion=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ4eHh4eHgiLCJleHAiOiIxNDk5OTQ3NjY4IiwiaXNzIjoibWUiLCJhdWQiOiJodHRwczovL2F1dGgtc2FuZGJveC5hYm5hbXJvLmNvbS9vYXV0aC90b2tlbiJ9.jGwHKG_YjgKpR8NPpaLu6nJ97obeP2vcxg6fOWBKdJ0&scope=tikkie'
// Load our pre-creaed private key PEM file.
// Note: Please share your public key along with your app name and developer email id at api.support@nl.abnamro.com.
// Token generation will not work unless public key is associated with your app.
let privkey = chilkat::PrivateKey::new();
if privkey.load_pem_file("qa_data/pem/abnAmroPrivateKey.pem").is_err() {
println!("{}", privkey.last_error_text());
return;
}
// Create the JWT.
let jwt = chilkat::Jwt::new();
// Create the header:
// {
// "typ": "JWT",
// "alg": "RS256"
// }
let json_header = chilkat::JsonObject::new();
let _ = json_header.update_string("typ", "JWT");
let _ = json_header.update_string("alg", "RS256");
// Create the payload:
// {
// "nbf": 1499947668,
// "exp": 1499948668,
// "iss": "me",
// "sub": "anApiKey",
// "aud": "https://auth-sandbox.abnamro.com/oauth/token"
// }
let json_payload = chilkat::JsonObject::new();
let cur_date_time = jwt.gen_numeric_date(0);
// Set the "not process before" timestamp to now.
let _ = json_payload.add_int_at(-1, "nbf", cur_date_time).is_ok();
// Set the timestamp defining an expiration time (end time) for the token
// to be now + 1 hour (3600 seconds)
let _ = json_payload.add_int_at(-1, "exp", cur_date_time + 3600).is_ok();
let _ = json_payload.update_string("iss", "me");
let _ = json_payload.update_string("sub", "anApiKey");
let _ = json_payload.update_string("aud", "https://auth-sandbox.abnamro.com/oauth/token");
// Produce the smallest possible JWT:
jwt.set_auto_compact(true);
let Ok(jwt_str) = jwt.create_jwt_pk(&json_header.emit().unwrap_or_default(), &json_payload.emit().unwrap_or_default(), &privkey) else {
println!("{}", jwt.last_error_text());
return;
};
let http = chilkat::Http::new();
let req = chilkat::HttpRequest::new();
req.add_param("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
req.add_param("grant_type", "client_credentials");
req.add_param("client_assertion", &jwt_str);
req.add_param("scope", "tikkie");
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
let resp = chilkat::HttpResponse::new();
if http.http_req("https://api-sandbox.abnamro.com/v1/oauth/token", &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
if resp.status_code() != 200 {
println!("{}", resp.body_str());
return;
}
// Get the JSON result:
// {
// "access_token": "{your access token}",
// "expires_in": "{duration of validity in seconds}",
// "scope": "{scope(s) for which the access token is valid}",
// "token_type": "{it is always Bearer}"
// }
let json = chilkat::JsonObject::new();
let _ = json.load(&resp.body_str());
println!("access_token: {}", json.string_of("access_token").unwrap_or_default());
println!("token_type: {}", json.string_of("token_type").unwrap_or_default());
println!("expires_in: {}", json.string_of("expires_in").unwrap_or_default());
println!("scope: {}", json.string_of("scope").unwrap_or_default());