Downloading files with Microsoft Graph using bash (curl)

I try to download several files from OneDrive through Microsoft Graph.

I am very close to achieve my goal. For the moment I have managed the token system (notably through Jay Lee detailed answer), and achieved to resolve the confusion that I made among the different endpoints that exist in Microsoft Graph (thank you Marc LaFleur - MSFT).

Now I really work on the call of the API that would permit to download the files I want. This is how I proceeded:

1- As I've seen in the Microsoft Graph Documentation, the normal syntax would be the following:

curl -w %{time_total} https://graph.microsoft.com/v1.0/me/drive/items/01M...WU/content -H "Authorization: Bearer $access_token"

2- However, this gives me a 302 request, which I can't handle in bash. So I looked for another solution and I found this Microsoft article explaining that:

"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 @microsoft.graph.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."

It talks about Javascript but I think it can be applied to my case.

3- So I tried this method and I wrote:

curl "https://graph.microsoft.com/v1.0/me/drive/items/01MB...WU?select=id,@microsoft.graph.downloadUrl" -H "Authorization: Bearer $access_token"

It gave me the URL that normally would permit me to finally download the file, but when I execute it, I get the following response:

Code:

curl "https://graph.microsoft.com/v1.0/$metadata#users('e967dd4d-680e-4a06-9bf7-468875e1a04c')/drive/items/$entity" -H "Authorization: Bearer $access_token" (it is exactly what I got with the previous request)

Response:

enter image description here

Any help? Why is it a bad request since I've put exactly what I got from the graph.microsoft.com request?

Upvotes: 1

Views: 2669

Answers (1)

Brad
Brad

Reputation: 4192

https://graph.microsoft.com/v1.0/$metadata#users('e967dd4d-680e-4a06-9bf7-468875e1a04c')/drive/items/$entity is the @odata.context annotation, which is not what you're after. You need to pull the URL out of the @microsoft.graph.downloadUrl annotation and use that to grab the file contents. The URL you're after should mention download.aspx in it.

Upvotes: 2

Related Questions