Downloading a publicly-shared file from OneDrive

前端 未结 1 1447
Happy的楠姐
Happy的楠姐 2021-01-14 20:48

When I create a share link in the UI with the \"Anyone with this link can view this item\" option, I get a URL that looks like https://onedrive.live.com/redir?resid=XX

1条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 21:46

    Have a read over https://dev.onedrive.com - it documents how you can make a query to our service to get the metadata for an item, along with URLs that can be used to directly download the content.

    Update with more details

    Sorry, the documentation you need for your specific scenario is still in process (along with the associated SDK changes) so I'll give you an overview of how to do it.

    There's a sibling to the /drives path called /shares which accepts a sharing URL (such as the one you have above) in an encoded format and allows you to get metadata for the item it represents. This does not require authentication provided the sharing URL has a valid authkey.

    The encoding scheme for the id is u!, where follows the guidelines outlined here (trim the = characters from the end).

    Here's a snippet that should give you an idea of the whole process:

    string originalUrl = "https://onedrive.live.com/redir?resid=XXX!YYYY&authkey=!foo";
    byte[] urlAsUtf8Bytes = Encoding.UTF8.GetBytes(originalUrl);
    string utf8BytesAsBase64String = Convert.ToBase64String(urlAsUtf8Bytes);
    string encodedUrl = "u!" + utf8BytesAsBase64String.TrimEnd('=').Replace('/', '_').Replace('+', '-');
    
    string metadataUrl = "https://api.onedrive.com/v1.0/shares/" + encodedUrl + "/root";
    

    From there you can append /content if you want to get the contents of the file, or you can start navigating through if the URL represents a folder (e.g. /children/childfile.txt)

    0 讨论(0)
提交回复
热议问题