Onedrive cors download in javascript

若如初见. 提交于 2019-12-05 07:09:59

The answer, as best as I can tell, is that downloading content cannot be done purely by JavaScript in a browser. Why did they do it this way? You'd have to ask them, but I would guess either a bug, or some unspecified "security concerns". For what it's worth, they seem to think that downloading content is CORS compliant in the documentation here: https://dev.onedrive.com/misc/working-with-cors.htm:

To download files from OneDrive in a JavaScript app you cannot use the /content API, since this responds with a 302 redirect. A 302 redirect is explicitly prohibited when a CORS preflight is required, such as when providing the Authorization header.

Instead, your app needs to select the @content.downloadUrl property, which returns the same URL that /content would have redirected to. This URL can then be requested directly using XMLHttpRequest. Because these URLs are pre-authenticated they can be retrieved without a CORS preflight request.

However, to the best of my knowledge, they are wrong. Just because you don't need a preflight request doesn't mean that the response is CORS-compliant. You still need an Access-Control-Allow-Origin header on the response.

For anyone wondering, this is still an issue in the new Graph API (which is essentially a proxy API to the OneDrive API, as I understand it). The same basic issue is still present - you can get a download URL from your items, but that URL points to a non-CORS-compliant resource, so it doesn't do you a whole lot of good.

I have an active issue open with Microsoft here about this issue. There has been some response to my issue (I got them to expose the download URL through the graph API), but I'm still waiting to see if they'll come up with a real solution to downloading content from JavaScript.

If I get a solution or real answer on that issue, I'll try to report back here so others in the future can have a real answer to reference.

This is not an answer, I cannot comment yet.

Last week the new API for onedrive was released. http://onedrive.github.io/index.htm

Unfortunately it will not solve the problem.

https://api.onedrive.com/v1.0/drive/root:{path and name}:/content?access_token={token}

Will still redirect to a ressource somewhere at https://X.files.1drv.com/.X.

Which will not contain any Access-Control-Allow-Origin headers. Same goes for the Url "@content.downloadUrl" in the JSON response.

I hope that microsoft will address this problem in the very near future, because the API is at the moment of very limited use, since you cannot process file contents from onedrive with html5 apps. Apart from the usual file browser.

The only solution, which I see at the moment would be a chrome app, which can process the Url without CORS. see https://developer.chrome.com/apps/angular_framework

Box does exactly the same thing for download requests. I have not found any way around this problem without involving a server because the browser will not let your program get access to the contents of the 302 redirect response. For security reasons I am not convinced of, browsers mandatorily follow redirect requests without allowing user intervention.

The way we finally worked around this was

  1. the browser app sends the GET request to the server which forwards it to the cloud provider (box/ondrive).
  2. server then DOES NOT follow the 302 redirect response from Box or OneDrive
  3. The server instead returns to the browser app, the content of the location field in the 302 response header, which contains the download url
  4. The javascript in the browser app then downloads the file using the url.

You can now just use the "@content.downloadUrl" property of the item in your GET request. Then there is no redirection.

From https://dev.onedrive.com/items/download.htm:

Returns a 302 Found response redirecting to a pre-authenticated download URL for the file. This is the same URL available through the @content.downloadUrl property on an item.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!