简体   繁体   中英

How can I get share and download links from OneDrive API

I am using the OneDrive JS Picker and would like to get both a download and share value. The sample in the documentation lists the options for the action parameter separated by pipes. I was hoping to use bitwise operators to combine those values (eg

action: "share | download"

but that doesn't seem to allow both the webUrl value and the download url (@microsoft.graph.downloadUrl)... has anybody else been able to do this? Or is there a way to use the share URL (webUrl) to get a download url so we can get the text contents of files?

I know there is a REST service available though that may be deprecated, given the message at the top of that page:

This documentation is archived and is not being maintained.

but if that is still usable, perhaps we could use that to download the file... if so, can I put the file id in that download URL and an access token?

I was able to host the OneDrive.js file locally, un-minify it and modify the line below (line 104) to have the downloadUrl included in all queries on files. That allows us to get a shareUrl and a downloadUrl.

e.DEFAULT_QUERY_ITEM_PARAMETER = "expand=thumbnails&select=id,name,size,webUrl,folder,@content.downloadUrl";

sorry for the missleading pipe sign. Actually right now we only allow one action per request like (action: "query"). The pipeline sign in the doc means 'or' in English not in code.

Before my answer, I want to clarify the difference between a webUrl and a shareUrl.

  • A webUrl which you see in the response's 'webUrl' attribute is the url pointed to the resource online which requires user login to see it.

  • A shareUrl which contains the permission which everyone who has the link could see/edit it based on the user's config.

JS Picker

If you want the download link and a webUrl back at the same time, it should be easy:

{
    action: "query",
    advanced: {queryParameters: "select=id,name,webUrl,@content.downloadUrl"}
}

If you want both the download link and share link back at the same time, it is not supported.

API

For using the API to achieve this, you can go to the new API page: https://dev.onedrive.com/items/get.htm

and get the item with already shared links should be

GET /v1.0/drive/items/<item-id>?select=id,name,@content.downloadUrl,webUrl&expand=permissions

all already shared links should be returned in the permissions array object.

If you don't have a shared link, and you need to create the shared link, you should try https://dev.onedrive.com/items/sharing_createLink.htm

while it does not return the webUrl and download url back so you need an extra request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM