0
votes

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.
1
I tried searching for relevant sources at least to get started working one using OneDrive APIs. I found (this example) [gist.github.com/tanaikech/22bfb05e61f0afb8beed29dd668bdce9] by Tanaikech which is about upload of a file. I am able to generate refresh_tokens and do authentication. However, still not able to upload as it shows "Access Denied". I have cross-checked that Files.ReadWrite.all scope is also included. Not really able to crack this further.mrhagrawal
Did you include the scope both in the app registration in AAD as well as in the call to get the bearer token (or use graph.microsoft.com/.default)? I think people will need to see more about what you're sending in the call (embed code in the question, don't send links). Also that link in comment isn't working, fyi.socasanta
@socasanta, Yes, I have included the scopes both the times during the app registration in AAD and during my API call as well. Also, Thanks for pointing out that the link isn't working. This is the correct link: gist.github.com/tanaikech/… And, I chose not to mention that code in the question itself so as to not deviate from the actual question as my end goal is to download file and this example was about uploading a file. I wanted to refer this as an example to the kind of solution I am looking for my purpose.mrhagrawal
You should have 2 tokens coming in the /token response - access_token and refresh_token. You should be using access_token (not refresh_token) for the operations. (You said refresh_token, so I'm clarifying).socasanta
True. For each operation, I will be needing access tokens but as they are short-lived so, for the greater good, I referred to refresh token as they can be used to get fresh access tokens for each operation. Thanks for making it more clear. :) Any help on listing the files from OneDrive or/and downloading files?mrhagrawal

1 Answers

1
votes

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://docs.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http https://docs.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http