Sample code for 30+ languages & platforms
Lua

Retrieve the metadata for a DriveItem

See more OneDrive Examples

Fetches the JSON metadata for a DriveItem.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

-- 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()

-- Sends the following GET request:
-- GET https://graph.microsoft.com/v1.0/users/{user-id}/drive/root:/{item-path}

-- Make sure to automatically follow redirects
http.follow_redirects = true

-- This example will get the metadata for /Misc/wildlife/penguins.jpg
http:set_url_var("item_path", "Misc//penguins.jpg")
http:set_url_var("user_id", "4fe732c3-322e-4a6b-b729-2fd1eb5c6104")

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

json.emit_compact = false
local success = json:load(metaData)
print(json:emit())

-- Sample JSON metadata result:

-- {
--   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/drive/root/$entity",
--   "@microsoft.graph.downloadUrl": "https://public.dm.files.1drv.com/y4mh0z_Og97O7Q...o2q1HhNBU",
--   "createdDateTime": "2017-06-04T20:40:22.48Z",
--   "cTag": "aYzozQTMzRkNFQjlCNzRDQzE1ITQ4NzIuMjU3",
--   "eTag": "aM0EzM0ZDRUI5Qjc0Q0MxNSE0ODcyLjY",
--   "id": "3A33FCEB9B74CC15!4872",
--   "lastModifiedDateTime": "2018-10-20T18:22:29.977Z",
--   "name": "penguins.jpg",
--   "size": 777835,
--   "webUrl": "https://1drv.ms/i/s!ABXMdJvr_DM6pgg",
--   "rating": {
--     "rating": 75,
--     "simpleRating": 4
--   },
--   "createdBy": {
--     "user": {
--       "displayName": "Joe Programmer",
--       "id": "3a33fceb9b74cc15"
--     }
--   },
--   "lastModifiedBy": {
--     "user": {
--       "displayName": "Joe Programmer",
--       "id": "3a33fceb9b74cc15"
--     }
--   },
--   "parentReference": {
--     "driveId": "3a33fceb9b74cc15",
--     "driveType": "personal",
--     "id": "3A33FCEB9B74CC15!4871",
--     "name": "wildlife",
--     "path": "/drive/root:/Misc/wildlife"
--   },
--   "file": {
--     "mimeType": "image/jpeg",
--     "hashes": {
--       "sha1Hash": "DF7BE9DC4F467187783ACA68C7CE98E4DF2172D0"
--     }
--   },
--   "fileSystemInfo": {
--     "createdDateTime": "2017-06-04T20:40:22.48Z",
--     "lastModifiedDateTime": "2009-07-14T05:32:31.674Z"
--   },
--   "image": {
--     "height": 768,
--     "width": 1024
--   },
--   "photo": {
--     "takenDateTime": "2008-02-18T05:07:31Z"
--   },
--   "shared": {
--     "scope": "users",
--     "owner": {
--       "user": {
--         "displayName": "Joe Programmer",
--         "id": "3a33fceb9b74cc15"
--       }
--     }
--   }
-- }
--

-- If the response status code was not 200, then it failed.
if http.last_status ~= 200 then
    print("Response Status Code = " .. http.last_status)
    print("Failed.")
    return
end

-- Demonstrate how to parse the JSON...

local odata_context = json:string_of("\"@odata.context\"")
local microsoft_graph_downloadUrl = json:string_of("\"@microsoft.graph.downloadUrl\"")
local createdDateTime = json:string_of("createdDateTime")
local cTag = json:string_of("cTag")
local eTag = json:string_of("eTag")
local id = json:string_of("id")
local lastModifiedDateTime = json:string_of("lastModifiedDateTime")
local name = json:string_of("name")
local size = json:int_of("size")
local webUrl = json:string_of("webUrl")
local ratingRating = json:int_of("rating.rating")
local ratingSimpleRating = json:int_of("rating.simpleRating")
local createdByUserDisplayName = json:string_of("createdBy.user.displayName")
local createdByUserId = json:string_of("createdBy.user.id")
local lastModifiedByUserDisplayName = json:string_of("lastModifiedBy.user.displayName")
local lastModifiedByUserId = json:string_of("lastModifiedBy.user.id")
local parentReferenceDriveId = json:string_of("parentReference.driveId")
local parentReferenceDriveType = json:string_of("parentReference.driveType")
local parentReferenceId = json:string_of("parentReference.id")
local parentReferenceName = json:string_of("parentReference.name")
local parentReferencePath = json:string_of("parentReference.path")
local fileMimeType = json:string_of("file.mimeType")
local fileHashesSha1Hash = json:string_of("file.hashes.sha1Hash")
local fileSystemInfoCreatedDateTime = json:string_of("fileSystemInfo.createdDateTime")
local fileSystemInfoLastModifiedDateTime = json:string_of("fileSystemInfo.lastModifiedDateTime")
local imageHeight = json:int_of("image.height")
local imageWidth = json:int_of("image.width")
local photoTakenDateTime = json:string_of("photo.takenDateTime")
local sharedScope = json:string_of("shared.scope")
local sharedOwnerUserDisplayName = json:string_of("shared.owner.user.displayName")
local sharedOwnerUserId = json:string_of("shared.owner.user.id")