Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.97+

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.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local success = false

-- 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
local json = chilkat.JsonObject.new()
json:update_string("client_id", "2871da2c-8176-4b7f-869b-2311aa82e743")
json:update_string("client_secret", "2hu9Q~r5QuryUcEkNbg1btLtnfU1VUXzhSCG6brH")
json:update_string("scope", "https://graph.microsoft.com/.default")
json:update_string("token_endpoint", "https://login.microsoftonline.com/114d7ed6-71bf-4dbe-a866-748364121bf2/oauth2/v2.0/token")

local http = chilkat.Http.new()
http.auth_token = json:emit()

-- 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.)
http:set_url_var("item_path", "/TestDir")
http:set_url_var("user_id", "4fe732c3-322e-4a6b-b729-2fd1eb5c6104")

local resp = http:quick_get_str("https://graph.microsoft.com/v1.0/users/{$user_id}/drive/root:{$item_path}:/children")
if not http.last_method_success then
    print(http.last_error_text)
    return
end

-- The response should be JSON.
json.emit_compact = false
json:load(resp)

-- A successful response should return a status code of 200.
if http.last_status ~= 200 then
    print(json:emit())
    print("Response status = " .. http.last_status)
    return
end

print(json:emit())

local lastMod = chilkat.DateTime.new()
local photoTaken = chilkat.DateTime.new()

-- Iterate over the DriveItems in the JSON response:
local i = 0
local numItems = json:size_of_array("value")
while i < numItems do
    json.i = i
    print("-- DriveItem " .. (i + 1))
    print("id: " .. json:string_of("value[i].id"))
    print("name: " .. json:string_of("value[i].name"))
    print("size: " .. json:int_of("value[i].size"))

    -- Get the lastModifiedDateTime
    lastMod:set_from_timestamp(json:string_of("value[i].fileSystemInfo.lastModifiedDateTime"))

    -- Is this a folder?
    if json:has_member("value[i].folder") then
        print("This is a folder with " .. json:int_of("value[i].folder.childCount") .. " children")
    end

    if json:has_member("value[i].file") then
        print("This is a file.")
        print("SHA1 hash: " .. json:string_of("value[i].file.hashes.sha1Hash"))
        print("mimeType: " .. json:string_of("value[i].mimeType"))
    end

    if json:has_member("value[i].image") then
        print("This is an image.")
        print("height: " .. json:int_of("value[i].image.height"))
        print("width: " .. json:int_of("value[i].image.width"))
    end

    if json:has_member("value[i].photo") then
        print("This is a photo.")
        photoTaken:set_from_timestamp(json:string_of("value[i].photo.takenDateTime"))
        print("photo taken on " .. photoTaken:get_as_rfc822(true))
    end

    if json:has_member("value[i].audio") then
        print("This is an audio file.")
        print("duration: " .. json:int_of("value[i].audio.duration"))
    end

    i = i + 1
end