Sample code for 30+ languages & platforms
Swift

Retrieve the metadata for a DriveItem

See more OneDrive Examples

Fetches the JSON metadata for a DriveItem.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let http = CkoHttp()!
    http.authToken = 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.followRedirects = true

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

    var metaData: String? = http.quickGetStr(url: "https://graph.microsoft.com/v1.0/users/{$user_id}/drive/root:/{$item_path}")
    if http.lastMethodSuccess != true {
        print("\(http.lastErrorText!)")
        return
    }

    json.emitCompact = false
    success = json.load(json: 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.lastStatus.intValue != 200 {
        print("Response Status Code = \(http.lastStatus.intValue)")
        print("Failed.")
        return
    }

    // Demonstrate how to parse the JSON...

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

}