Sample code for 30+ languages & platforms
Rust

Search for Files in Google Drive

See more Google Drive Examples

This example follows the same methodology for listing all files in Google Drive in pages, but applies a search filter. It shows how to apply a query parameter for filtering the file results. See the Google Drive Files list for more optional HTTP parameters.

Chilkat Rust Downloads

Rust

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 5 results per page for testing.  (The default page size is 100, with a max of 1000.
let _ = rest.add_query_param("pageSize", "5");

// Our search filter is to list all files containing ".jpg" (i.e. all JPG image files)
let _ = rest.add_query_param("q", "name contains '.jpg'");

let json = chilkat::JsonObject::new();
let mut i: i32 = 0;
let mut num_files: i32 = 0;

// Send the request for the 1st page.
let mut json_response = rest.full_request_no_body("GET", "/drive/v3/files").unwrap_or_default();

let mut page_number = 1;
let mut page_token = String::new();
let mut b_continue_loop = rest.last_method_success() && (rest.response_status_code() == 200);

while b_continue_loop {

    println!("---- Page {} ----", page_number);

    // Iterate over each file in the response and show the name, id, and mimeType.
    let _ = json.load(&json_response);

    num_files = json.size_of_array("files");
    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;
    }

    // Get the next page of files.
    // If the "nextPageToken" is present in the JSON response, then use it in the "pageToken" parameter
    // for the next request.   If no "nextPageToken" was present, then this was the last page of files.
    page_token = json.string_of("nextPageToken").unwrap_or_default();
    b_continue_loop = false;
    let b_has_more_pages = json.last_method_success();
    if b_has_more_pages {
        let _ = rest.clear_all_query_params();
        let _ = rest.add_query_param("pageSize", "5");
        let _ = rest.add_query_param("pageToken", &page_token);
        let _ = rest.add_query_param("q", "name contains '.jpg'");

        json_response = rest.full_request_no_body("GET", "/drive/v3/files").unwrap_or_default();
        b_continue_loop = rest.last_method_success() && (rest.response_status_code() == 200);
        page_number = page_number + 1;
    }

}

if !rest.last_method_success() {
    println!("{}", rest.last_error_text());
    return;
}

// A successful response will have a status code equal to 200.
if rest.response_status_code() != 200 {
    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;
}