How do I download a file from a OneDrive URL?

Jim Kelly 1 Reputation point
2021-12-08T19:53:08.31+00:00

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 Security | Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. CarlZhao-MSFT 46,376 Reputation points
    2021-12-09T06:38:49.623+00:00

    Hi @Jim Kelly

    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.


  2. M.Pahulje 1 Reputation point
    2022-09-02T13:12:46.693+00:00

    You can use graph to execute generate an embed link, and add download to end of the string &download=1. Test the download URL here https://metadataconsulting.blogspot.com/2019/03/OneDrive-2019-Direct-File-Download-URL-Maker.html.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.