Search code examples
microsoft-graph-apionedrive

Access OneDrive Directories and files using MSGraph API with client Authentication


I have been trying to use the MSGraph API to access files in a shared OneDrive which is part of a 365 account. I need to create an app that gets client permissions and then makes the API calls. Specifically this must be done with client authentication (not delegated - on behalf of a signed in user)

The question is how can I get a directory list of what's in my oneDrive using MSGraph? Then I need to be able to download specific items.

The steps I did are

  1. Register an Azure and copied the account details and secret (works fine) added all the permissions I need.
  2. In Postman, I requested the required Auth2 bearer token (works fine)
https://login.microsoftonline.com/{{TenantID}}/oauth2/V2.0/token

This correctly returns a bearer token

  1. In postman I used this to get my drives https://graph.microsoft.com/v1.0/drives/ Shows one drive with Name "Documents" and an id value
  2. In postman I tried to list the items in the drives with
https://graph.microsoft.com/{{driveId}}/items

This returns: "code": "invalidRequest", "message": "The 'filter' query option must be provided."

  1. In postMan I tried to search to find a specific file I know is in the oneDrive
GET /drives/{drive-id}/root/search(q='todo.txt')

This returns and empty array [] - when I can see that the drive contains this file

The question is how can I get a directory list of what's in my oneDrive using MSGraph? Then I need to be able to download specific items.

EDIT: I got the drive id from this Postman request: https://graph.microsoft.com/v1.0/drives which returned: "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives", "value": [ { "createdDateTime": "2022-02-12T14:09:19Z", "description": "", "id": "b!3JFpYb83RUidS4Xkt71Orcfzp9sQsf1Kmr_43UsnyqL4nAuvo6oYRIAe-gp_shCu", "lastModifiedDateTime": "2023-01-10T03:57:29Z", "name": "Documents", "webUrl": "https://agcwainc.sharepoint.com/Shared%20Documents",

I then used the id to try to find its contents: https://graph.microsoft.com/v1.0/drives/{{driveID}}/root/children which returns and empty array: "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('b%213JFpYb83RUidS4Xkt71Orcfzp9sQsf1Kmr_43UsnyqL4nAuvo6oYRIAe-gp_shCu')/root/children", "value": []

The app permissions are: App Permissions

The Onedrive contents is: OneDrive Contents


Solution

  • First validate your App Registration has the correct permissions and Admin Consent has been granted.

    API / Permission Name Type Description
    Files.Read.All Application If you only need to read files
    Files.ReadWrite.All Application If you need to read and upload files

    To get all files at the root level of the a drive you can use

    https://graph.microsoft.com/v1.0/drives/{driveId}/root/children
    

    Get all files inside of a folder of a drive

    https://graph.microsoft.com/v1.0/drives/{driveId}/root:/{folderName}:/children
    

    Reference: https://learn.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http

    Note: See a full answer below from bcperth based on info provided by Brian Smith.