question

JimKelly-5033 avatar image
0 Votes"
JimKelly-5033 asked saldana-msft edited

How do I download a file from a OneDrive URL?

I'm trying to use the msgraph api to do this:

  1. Logged-in user opens an Excel Online file and copies the URL from the address bar.

  2. User goes to my app, authenticates with MS, and pastes URL into a form in my app.

  3. My app GETs the file contents and does wonderful things with it.

I discovered I can't simply curl the URL with the access token, that gives a 401 error. All the MSGraph REST APIs want a drive ID and an item ID or a file path, not the webUrl of a DriveItem. Although the webUrl seems to encode an eTag ID that can be searched through the api, I would hope there's a more universal way to locate the resource from a universal resource locator.

Is there some reliable way to convert a webUrl to IDs MSGraph's API can understand? Or some other way to GET the contents of the file?

microsoft-graph-filesmicrosoft-graph-workbooks
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

CarlZhao-MSFT avatar image
0 Votes"
CarlZhao-MSFT answered CarlZhao-MSFT commented

Hi @JimKelly-5033

You definitely need the item id to download the item file, because id is the unique identifier of the item within the drive. The webUrl is just the address value of the item file displayed in the browser, obviously it cannot refer to this item file. In addition, webUrl cannot be converted to item id either.

So you need to get the item id first GET https://graph.microsoft.com/v1.0/me/drive/root/children?$select=id,name, and then download the item file according to the item id GET https://graph.microsoft.com/v1.0/me/drive/items/{item-id}/content.


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JimKelly-5033 Would you please provide us with an update on the status of your issue?

0 Votes 0 ·