Onedrive cors download in javascript

ε祈祈猫儿з 提交于 2020-02-02 00:32:48

问题


I'm trying to process onedrive files in client-side javascript, but first I need a way to use XMLHttpRequest to download the file. Onedrive supports cors for a lot of operations, but for downloading the file into javascript there is the following problem:

As mentioned here: onedrive rest api manual

I can send a request to:

GET https://apis.live.net/v5.0/FILE_ID/content?access_token=ACCESS_TOKEN

and it will reply with a location header redirecting the browser to the file. The problem is when I send these requests through XHR, the browser always sends the Origin header with the request. For the first request I described above, onedrive also replies with an Access-Control-Allow-Origin:* header, so the request is allowed in the browser. However, when the browser is redirected to the actual location of the file, that resource does not have the Access-Control-Allow-Origin header, so the XHR request is denied by the browser(chrome sends an Origin header set to null for the redirect request).

I've also tried getting the location but not redirecting automatically, and then sending another XHR request, this will set the origin header to the domain of my site, but the result is the same.

As I mentioned in the beginning, I need to process the data in javascript, so I'm not asking about how to download onedrive files to hard drive. I need the data to be accessible by javascript in the webpage.

I know that I can use server side programming to get the file data for me and then send it to the client, but for my application this is not an option(at least this is not what I'm asking for at the moment).

If there is no way to do this, does anyone have an idea why they would implement their api this way? To allow javascript to get the location through cors and redirect but not include a cors header for the redirected resource. Why not just deny cors in the first place? Is this a bug?


回答1:


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.




回答2:


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




回答3:


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.



回答4:


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.



来源:https://stackoverflow.com/questions/27068647/onedrive-cors-download-in-javascript

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