Rust Requires Chilkat v11.0.0+
Rust
List Gmail message IDs across all pages
See more GMail REST API Examples
List message and thread IDs for the authenticated user. Continue with nextPageToken until it is absent. A message ID is a Gmail identifier, distinct from the RFC Message-ID header. Use it in the reading examples. Spam and Trash are excluded by default.
Authorize the application with https://www.googleapis.com/auth/gmail.readonly. 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();
let mut total = 0;
let base_url = chilkat::StringBuilder::new();
let _ = base_url.append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100");
let url = chilkat::StringBuilder::new();
let page_token = chilkat::StringBuilder::new();
let mut more = true;
while more {
let _ = url.set_string(&base_url.get_as_string().unwrap_or_default());
if page_token.length() > 0 {
let _ = url.append("&pageToken=");
let _ = url.append(&http.url_encode(&page_token.get_as_string().unwrap_or_default()).unwrap_or_default());
}
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;
}
// Gmail can omit messages when the search or mailbox is empty.
let count = reply.size_of_array("messages");
let mut i = 0;
while i < count {
reply.set_i(i);
println!("{} thread {}", reply.string_of("messages[i].id").unwrap_or_default(), reply.string_of("messages[i].threadId").unwrap_or_default());
total = total + 1;
i = i + 1;
}
more = false;
if reply.json_type_of("nextPageToken") == 1 {
let _ = page_token.set_string(&reply.string_of("nextPageToken").unwrap_or_default());
more = page_token.length() > 0;
}
}
println!("Messages listed: {}", total);