mrhagrawal
mrhagrawal

Reputation: 11

Download File from OneDrive using REST API

I am trying to find a way out using node js to fetch the list of files (xlsx and CSV files are of my interest) from User's Microsoft OneDrive. From the list, choose the file and then download it into the local system.

I can see Microsoft documentation about using OneDrive REST API here. But, since I am new at this, I am not really able to work this around. Any help will be appreciated.

I want to do something similar to what we can do with Google Drive where I could get a list of files along with their names and unique id and when the user chooses one file, by use of the unique id, I was able to download the required file. I am wondering if a similar thing can be done with OneDrive.

My progress so far:

  1. I am able to ask users to provide their consent and get code in return to redirect Uri (localhost in my case).

The link that does this- https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={my_client_id}&scope=User.Read%20offline_access%20Files.ReadWrite.all%20Files.ReadWrite&response_type=code&response_mode=query&redirect_uri=https://localhost:3400

  1. After getting the code, I exchanged it for refresh token.

To do this exchange, I used Postman for the POST request. The URL for that is https://login.microsoftonline.com/common/oauth2/v2.0/token?grant_type=authorization_code&client_id=your_app_client_id&code=use_the_code_returned_on_previous_step

  1. Now that I have a refresh token, I want to do operations like listing the files on my OneDrive and select anyone to download.

Upvotes: 1

Views: 12134

Answers (1)

socasanta
socasanta

Reputation: 1561

I suggest looking into the Graph API as follows, and using the same auth_token received above. Instead of /me you can substitute a user ID as well.

Get information about the user's drive: GET https://graph.microsoft.com/v1.0/me/drive

Get the root folder of the drive: GET https://graph.microsoft.com/v1.0/me/drive/root/

Note: the documentation and the paths in the JSON results say "root:" but I haven't been able to get it work work with the colon.

Embedded in the result you should see:

"folder": {
    "childCount": {whatever the count is}
  },

To see the files that are in the folder: GET https://graph.microsoft.com/v1.0/me/drive/root/children

Files will have an id and a name, both of which can be used to retrieve them:

"createdDateTime": "2020-07-05T18:08:37Z",
      "eTag": "\"{29C06DFA-92AE-48D5-AF3D-149EF959030F},1\"",
      "id": "01EC2X7VP2NXACTLUS2VEK6PIUT34VSAYP",
      "lastModifiedDateTime": "2020-07-05T18:08:37Z",
      "name": "wizard of wor.png",

To download a file by ID: https://graph.microsoft.com/v1.0/me/drive/items/01EC2X7VP2NXACTLUS2VEK6PIUT34VSAYP/content

Or by path (in example below Wizard of Wor.png is the file name): https://graph.microsoft.com/v1.0/me/drive/root/children/Wizard%20of%20Wor.png/content

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

Upvotes: 4

Related Questions