Rust
Rust
Get Google API Access Token using JSON Private Key
See more Google APIs Examples
Demonstrates how to get a Google API access token using a JSON service account private 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 JSON key into a string.
let fac = chilkat::FileAccess::new();
let Ok(json_key) = fac.read_entire_text_file("qa_data/googleApi/chilkat25-b4214220e565.json", "utf-8") else {
println!("{}", fac.last_error_text());
return;
};
// A Google service account JSON private key looks like this:
// {
// "type": "service_account",
// "project_id": "chilkat25",
// "private_key_id": "b4214220f565881e19eeb97c2699bf5a0d1e3e0b",
// "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQ...NXcM=\n-----END PRIVATE KEY-----\n",
// "client_email": "chilkatsvc@chilkat25.iam.gserviceaccount.com",
// "client_id": "109122032928932715958",
// "auth_uri": "https://accounts.google.com/o/oauth2/auth",
// "token_uri": "https://oauth2.googleapis.com/token",
// "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
// "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/chilkatsvc%40chilkat25.iam.gserviceaccount.com",
// "universe_domain": "googleapis.com"
// }
let g_auth = chilkat::AuthGoogle::new();
g_auth.set_json_key(&json_key);
// Specify a scope.
g_auth.set_scope("https://mail.google.com/");
// Request an access token that is valid for this many seconds.
g_auth.set_expire_num_seconds(3600);
// When using a Google Workspace account with Gmail APIs, a service account can impersonate a user
// via a process called domain-wide delegation — and the "sub" claim in the JWT is what enables this.
// Domain-wide delegation allows a Google Workspace administrator to authorize a service account to
// act on behalf of any user in the domain, without user interaction.
// This is required for server-to-server access to user data — such as reading/sending Gmail from a background service.
// This is your company email address.
g_auth.set_sub_email_address("info@chilkat.xyz");
// Connect to www.googleapis.com using TLS
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:
let access_token = g_auth.access_token();
println!("Access Token: {}", access_token);
// Sample output:
// ya29.a0AW4XtxjGTD67Z8 .... IRw0218
// The access token allows us to send unlimited emails while it's valid. Once it expires, we must obtain and use a new one.
// -----------------------------------------------------------------------
let mailman = chilkat::MailMan::new();
// Set the properties for the GMail SMTP server:
mailman.set_smtp_host("smtp.gmail.com");
mailman.set_smtp_port(587);
mailman.set_start_tls(true);
mailman.set_smtp_username("info@chilkat.xyz");
mailman.set_o_auth2_access_token(&access_token);
// Create a new email object
let email = chilkat::Email::new();
email.set_subject("This is a test");
email.set_body("This is a test");
email.set_from("Chilkat Test <info@chilkat.xyz>");
let _ = email.add_to("Chilkat Software", "info@chilkatsoft.com").is_ok();
// To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.
if mailman.send_email(&email).is_err() {
println!("{}", mailman.last_error_text());
return;
}
if mailman.close_smtp_connection().is_err() {
println!("Connection to SMTP server not closed cleanly.");
}
println!("Successfully sent email using Gmail with a service account key.");