Access a Google Workspace mailbox with a service account
See more GMail REST API Examples
A Google Workspace super administrator must authorize the service account client ID for https://www.googleapis.com/auth/gmail.metadata in Admin console domain-wide delegation. Enable the Gmail API in the Cloud project. Set SubEmailAddress to a Gmail-enabled user in that organization. The profile response identifies the mailbox represented by me.
Store the downloaded JSON service-account key in gmail-service-account.json in a private working directory. AuthGoogle signs a JWT with the key and exchanges it for an access token. Repeat that exchange when the token expires. For other operations, change Scope and grant that scope in the Admin console. This workflow is for organization-controlled applications. Personal Gmail accounts can use offline authorization with a refresh token.
Chilkat Rust Downloads
let mut success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let key = chilkat::JsonObject::new();
success = key.load_file("gmail-service-account.json").is_ok();
if !success {
println!("Could not load the service-account key from the private directory.");
return;
}
let auth = chilkat::AuthGoogle::new();
auth.set_json_key(&key.emit().unwrap_or_default());
auth.set_scope("https://www.googleapis.com/auth/gmail.metadata");
auth.set_sub_email_address("user@example.com");
auth.set_expire_num_seconds(3600);
// Obtain a token for the named Workspace user. Keep the computer clock accurate.
let connection = chilkat::Socket::new();
connection.set_require_ssl_cert_verify(true);
success = connection.connect("www.googleapis.com", 443, true, 10000).is_ok();
if !success {
println!("Could not establish the Google token connection.");
return;
}
success = auth.obtain_access_token(&connection).is_ok();
let _ = connection.close(1000);
if !success {
println!("Token exchange failed. Check the key, delegated scopes, user, and computer clock.");
return;
}
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&auth.access_token());
let resp = chilkat::HttpResponse::new();
let reply = chilkat::JsonObject::new();
success = http.http_no_body("GET", "https://gmail.googleapis.com/gmail/v1/users/me/profile", &resp).is_ok();
if resp.status_code() == 0 {
println!("Gmail did not return an HTTP response.");
return;
}
if (!success) || ((resp.status_code() < 200) || (resp.status_code() >= 300)) {
println!("Gmail HTTP {}", resp.status_code());
if reply.load(&resp.body_str()).is_ok() {
println!("{}", reply.string_of("error.message").unwrap_or_default());
}
return;
}
if reply.load(&resp.body_str()).is_err() {
println!("Expected a Gmail JSON response.");
return;
}
if (reply.json_type_of("emailAddress") != 1) || (reply.string_of_equals("emailAddress", "", true)) {
println!("Expected emailAddress in the response.");
return;
}
println!("Authorized mailbox: {}", reply.string_of("emailAddress").unwrap_or_default());
println!("Messages: {}", reply.int_of("messagesTotal"));