Sample code for 30+ languages & platforms
Java

Retrieve the metadata for a DriveItem

See more OneDrive Examples

Fetches the JSON metadata for a DriveItem.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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
        JsonObject json = new JsonObject();
        json.updateString("client_id", "2871da2c-8176-4b7f-869b-2311aa82e743");
        json.updateString("client_secret", "2hu9Q~r5QuryUcEkNbg1btLtnfU1VUXzhSCG6brH");
        json.updateString("scope", "https://graph.microsoft.com/.default");
        json.updateString("token_endpoint", "https://login.microsoftonline.com/114d7ed6-71bf-4dbe-a866-748364121bf2/oauth2/v2.0/token");

        Http http = new Http();
        http.setAuthToken(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.setFollowRedirects(true);

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

        String metaData;
        try {
            metaData = http.quickGetStr("https://graph.microsoft.com/v1.0/users/{$user_id}/drive/root:/{$item_path}");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        json.setEmitCompact(false);
        json.load(metaData);
        System.out.println(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.getLastStatus() != 200) {
            System.out.println("Response Status Code = " + http.getLastStatus());
            System.out.println("Failed.");
            return;
        }

        // Demonstrate how to parse the JSON...

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