Rust
Rust
List Files in Google Drive
See more Google Drive Examples
Demonstrates how to list files in Google Drive.See Google Drive Files list for more optional HTTP parameters.
Chilkat Rust Downloads
let _ = true;
// It requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses a previously obtained access token having permission for the
// Google Drive scope.
let g_auth = chilkat::AuthGoogle::new();
g_auth.set_access_token("GOOGLE-DRIVE-ACCESS-TOKEN");
let rest = chilkat::Rest::new();
// Connect using TLS.
let b_auto_reconnect = true;
let _ = rest.connect("www.googleapis.com", 443, true, b_auto_reconnect).is_ok();
// Provide the authentication credentials (i.e. the access token)
let _ = rest.set_auth_google(&g_auth);
// Get 4 results per page. (The default page size is 100, with a max of 1000.
let _ = rest.add_query_param("pageSize", "4");
// This uses the Google Drive V3 API... (not V2)
let Ok(json_response) = rest.full_request_no_body("GET", "/drive/v3/files") else {
println!("{}", rest.last_error_text());
return;
};
// A successful response will have a status code equal to 200.
if rest.response_status_code() != 200 {
println!("request header = {}", rest.last_request_header());
println!("response status code = {}", rest.response_status_code());
println!("response status text = {}", rest.response_status_text());
println!("response header: {}", rest.response_header());
println!("response JSON: {}", json_response);
return;
}
// A successful response looks like this:
// {
// "kind": "drive#fileList",
// "files": [
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolenpjTEU4ekJlQUU",
// "name": "test",
// "mimeType": "application/vnd.google-apps.folder"
// },
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolRm4ycjZtdXhRaEE",
// "name": "starfish4.jpg",
// "mimeType": "image/jpeg"
// },
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolMWt2VzN0Qlo1UjA",
// "name": "hamlet2.xml",
// "mimeType": "text/xml"
// },
// ...
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolc3RhcnRlcl9maWxlX2Rhc2hlclYw",
// "name": "Getting started",
// "mimeType": "application/pdf"
// }
// ]
// }
// Iterate over each file in the response and show the name, id, and mimeType.
let json = chilkat::JsonObject::new();
let _ = json.load(&json_response);
// Show the full JSON response.
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
let num_files = json.size_of_array("files");
let mut i = 0;
while i < num_files {
json.set_i(i);
println!("name: {}", json.string_of("files[i].name").unwrap_or_default());
println!("id: {}", json.string_of("files[i].id").unwrap_or_default());
println!("mimeType: {}", json.string_of("files[i].mimeType").unwrap_or_default());
println!("-");
i = i + 1;
}