Sample code for 30+ languages & platforms
Rust

OneDrive -- List Non-Root Directory

See more OneDrive Examples

This gets the collection of DriveItem children for a non-root DriveItem. This is the same as for getting the children for the root DriveItem, except the URL includes the path to the desired non-root DriveItem.

Note: This example requires Chilkat v9.5.0.97 or greater.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// This example uses the OAuth client credentials flow.
// See How to Create an Azure App Registration for OAuth 2.0 Client Credentials

// Use your client ID, client secret, and tenant ID in the following lines
let json = chilkat::JsonObject::new();
let _ = json.update_string("client_id", "2871da2c-8176-4b7f-869b-2311aa82e743");
let _ = json.update_string("client_secret", "2hu9Q~r5QuryUcEkNbg1btLtnfU1VUXzhSCG6brH");
let _ = json.update_string("scope", "https://graph.microsoft.com/.default");
let _ = json.update_string("token_endpoint", "https://login.microsoftonline.com/114d7ed6-71bf-4dbe-a866-748364121bf2/oauth2/v2.0/token");

let http = chilkat::Http::new();
http.set_auth_token(&json.emit().unwrap_or_default());

// Send a Get request like this:
// GET /users/{user-id}/drive/root:/{item-path}:/children

// This example will get the DriveItems in /TestDir
// (In other words, we're getting the directory listing for /TestDir.)
let _ = http.set_url_var("item_path", "/TestDir");
let _ = http.set_url_var("user_id", "4fe732c3-322e-4a6b-b729-2fd1eb5c6104");

let Ok(resp) = http.quick_get_str("https://graph.microsoft.com/v1.0/users/{$user_id}/drive/root:{$item_path}:/children") else {
    println!("{}", http.last_error_text());
    return;
};

// The response should be JSON.
json.set_emit_compact(false);
let _ = json.load(&resp);

// A successful response should return a status code of 200.
if http.last_status() != 200 {
    println!("{}", json.emit().unwrap_or_default());
    println!("Response status = {}", http.last_status());
    return;
}

println!("{}", json.emit().unwrap_or_default());

let last_mod = chilkat::DateTime::new();
let photo_taken = chilkat::DateTime::new();

// Iterate over the DriveItems in the JSON response:
let mut i = 0;
let num_items = json.size_of_array("value");
while i < num_items {
    json.set_i(i);
    println!("-- DriveItem {}", i + 1);
    println!("id: {}", json.string_of("value[i].id").unwrap_or_default());
    println!("name: {}", json.string_of("value[i].name").unwrap_or_default());
    println!("size: {}", json.int_of("value[i].size"));

    // Get the lastModifiedDateTime
    let _ = last_mod.set_from_timestamp(&json.string_of("value[i].fileSystemInfo.lastModifiedDateTime").unwrap_or_default());

    // Is this a folder?
    if json.has_member("value[i].folder") {
        println!("This is a folder with {} children", json.int_of("value[i].folder.childCount"));
    }

    if json.has_member("value[i].file") {
        println!("This is a file.");
        println!("SHA1 hash: {}", json.string_of("value[i].file.hashes.sha1Hash").unwrap_or_default());
        println!("mimeType: {}", json.string_of("value[i].mimeType").unwrap_or_default());
    }

    if json.has_member("value[i].image") {
        println!("This is an image.");
        println!("height: {}", json.int_of("value[i].image.height"));
        println!("width: {}", json.int_of("value[i].image.width"));
    }

    if json.has_member("value[i].photo") {
        println!("This is a photo.");
        let _ = photo_taken.set_from_timestamp(&json.string_of("value[i].photo.takenDateTime").unwrap_or_default());
        println!("photo taken on {}", photo_taken.get_as_rfc822(true).unwrap_or_default());
    }

    if json.has_member("value[i].audio") {
        println!("This is an audio file.");
        println!("duration: {}", json.int_of("value[i].audio.duration"));
    }

    i = i + 1;
}