Rust
Rust
Get Google API Access Token using P12 Service Account Key
See more Google APIs Examples
Demonstrates how to get a Google API access token using a P12 service account key.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// --------------------------------------------------------------------------------
// For a step-by-step guide for setting up your Google Workspace service account,
// see Setup Google Workspace Account for Sending SMTP GMail from a Service Account
// --------------------------------------------------------------------------------
// First load the PKCS12 (.p12 / .pfx) into a PFX object.
let pfx = chilkat::Pfx::new();
if pfx.load_pfx_file("qa_data/pfx/chilkat25-cbd7b42afbd8.p12", "notasecret").is_err() {
println!("{}", pfx.last_error_text());
return;
}
let g_auth = chilkat::AuthGoogle::new();
let _ = g_auth.set_p12(&pfx).is_ok();
// The ISS is your service account email address ending in gserviceaccount.com.
let iss = "chilkatsvc@chilkat25.iam.gserviceaccount.com".to_string();
// The scope is always the following string:
let scope = "https://mail.google.com/".to_string();
// The sub is your company email address
let oauth_sub = "info@chilkat.xyz".to_string();
// The access token is valid for this number of seconds.
let num_sec = 3600;
g_auth.set_email_address(&iss);
g_auth.set_scope(&scope);
g_auth.set_expire_num_seconds(num_sec);
g_auth.set_sub_email_address(&oauth_sub);
// Connect to www.googleapis.com
let tls_sock = chilkat::Socket::new();
if tls_sock.connect("www.googleapis.com", 443, true, 5000).is_err() {
println!("{}", tls_sock.last_error_text());
return;
}
// Send the request to obtain the access token.
if g_auth.obtain_access_token(&tls_sock).is_err() {
println!("{}", g_auth.last_error_text());
return;
}
// Examine the access token:
println!("Access Token: {}", g_auth.access_token());