Rust Requires Chilkat v11.0.0+
Rust
Read selected headers for a page of Gmail messages
See more GMail REST API Examples
List up to five messages and fetch only From, Subject, and Date for each one. Request metadataHeaders once per header name. Read the returned headers by name because order and presence vary. This header preview leaves the MIME body on Gmail.
Authorize the application with https://www.googleapis.com/auth/gmail.metadata. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Rust Downloads
let mut success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let token = chilkat::JsonObject::new();
success = token.load_file("gmail-token.json").is_ok();
if !success {
println!("Could not read gmail-token.json from the private working directory.");
return;
}
if (token.json_type_of("access_token") != 1) || (token.string_of_equals("access_token", "", true)) {
println!("An OAuth access token is required.");
return;
}
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&token.string_of("access_token").unwrap_or_default());
let resp = chilkat::HttpResponse::new();
let reply = chilkat::JsonObject::new();
success = http.http_no_body("GET", "https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5", &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());
success = reply.load(&resp.body_str()).is_ok();
if success {
println!("{}", reply.string_of("error.message").unwrap_or_default());
}
return;
}
success = reply.load(&resp.body_str()).is_ok();
if !success {
println!("Expected a Gmail JSON response.");
return;
}
let ids = chilkat::StringTable::new();
let count = reply.size_of_array("messages");
let mut i = 0;
while i < count {
reply.set_i(i);
success = ids.append(&reply.string_of("messages[i].id").unwrap_or_default()).is_ok();
if !success {
println!("Could not retain a message ID.");
return;
}
i = i + 1;
}
let url = chilkat::StringBuilder::new();
i = 0;
while i < ids.count() {
let _ = url.set_string("https://gmail.googleapis.com/gmail/v1/users/me/messages/");
let _ = url.append(&ids.string_at(i).unwrap_or_default());
let _ = url.append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date");
success = http.http_no_body("GET", &url.get_as_string().unwrap_or_default(), &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;
}
println!("Message: {}", ids.string_at(i).unwrap_or_default());
let mut h = 0;
while h < reply.size_of_array("payload.headers") {
reply.set_i(h);
println!("{}: {}", reply.string_of("payload.headers[i].name").unwrap_or_default(), reply.string_of("payload.headers[i].value").unwrap_or_default());
h = h + 1;
}
i = i + 1;
}
println!("Messages inspected: {}", ids.count());