2

I tried to download a file with Grpah API.

Tested with API browser, https://graph.microsoft.io/en-us/graph-explorer#

Ran following request, got list of file/folder item information,

https://graph.microsoft.com/v1.0/me/drive/root/children

And for one of the file item, I was able to get item information.

https://graph.microsoft.com/beta/me/drive/items/_an_item_id

But following returns HTTP 404.

https://graph.microsoft.com/beta/me/drive/items/_an_item_id/content

What will be the cause for this symptom?

Locke
  • 315
  • 1
  • 3
  • 14
  • Did the `/content` request itself `404`, or did it redirect you (`302`) to a URL that then returned a `404`? – Brad Nov 02 '16 at 20:20
  • Thanks @Brad, yes, 302 returned first and then 404. And the found location is exactly the same is the "@microsoft.graph.downloadUrl" information for the File Item. – Locke Nov 02 '16 at 20:36
  • @Brad, so is it because just the graph Explorer doesn't support the redirection? I have authenticated my Azure registered app with Daemon mode, got Auth-Code. I tried to download the file in 'download url' with 'Bearer' HTTP header but it says 404. How do you download a file from that link? – Locke Nov 02 '16 at 20:40
  • You mentioned daemon mode, does this mean you're targeting a OneDrive for Business account with an app-only token? – Brad Nov 03 '16 at 16:15
  • @Brad Not sure what's 'App-only' token but I tried "Daemon or Server Application to Web API" auth flow described here. https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-scenarios/ Example is here https://github.com/Azure-Samples/active-directory-dotnet-daemon It basically uses Client Id and Client secret, that's all. – Locke Nov 03 '16 at 18:17

1 Answers1

1

It is not supported to perform request to https://graph.microsoft.com/beta/me/drive/items/<itemid>/content endpoint via Graph Explorer.

The request sent to https://graph.microsoft.com/beta/me/drive/items/<itemid>/content endpoint via Grath Explorer is issued as a preflight request. The server responds with 302-Redirect but redirects are not allowed for preflighted requests due to CORS behavior.

Here is a workaround on how to download a file

The following drive item resource request:

https://graph.microsoft.com/beta/me/drive/items/<itemId>

returns @microsoft.graph.downloadUrl annotation which contains the actual link to file resource.

Once the drive item resource is retrieved, perform another GET request to url from @microsoft.graph.downloadUrl to return the actual file.

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • Thanks. Strange but it seems it returns 404 for the download request if I use accesstoken by 'Daemon or Server Application' type, written in https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-scenarios/ But If I use the Acces token from following auth flow, it gives me the content data. https://dev.onedrive.com/auth/aad_oauth.htm The latter requires User-Login, and I login as a Global Admin user for a domain. The former requires no user login, because it's daemon. Does the AccessToken type affect the behavior for the file download request? – Locke Nov 03 '16 at 18:14
  • @Locke I have the same issue (404) retrieving a file using 'App-only' token. Have you been able to download the file without user interaction? – Martino Bordin Jan 26 '17 at 08:18
  • @MartinoBordin I'm afraid no. I think it's because they have some level of authentication, and under lower authentication level, we can not do much things like downloading contents. – Locke Jan 30 '17 at 20:15
  • @Locke It's a shame we cannot download the contents using graph via "daemon authentication"...anyway I've been able to download it using sharepoint credentials and WebUrl returned by graph. Check the answer here: http://stackoverflow.com/a/23687279/485732 – Martino Bordin Jan 30 '17 at 20:38
  • @MartinoBordin Congrats. But the main purpose I wanted to use Daemon Auth. type was to minimize the user interaction, not needing password information per each users. I am not using that Daemon Auth. type now. – Locke Jan 30 '17 at 20:43
  • 1
    With my method I don't have any user interaction..I just set up a "system" account in sharepoint and use it in my "Daemon" – Martino Bordin Jan 30 '17 at 20:46
  • How would one go about downloading ``OneNote`` notebook if ``DriveItem`` does not expose ``file`` property or ``@microsoft.graph.downloadUrl`` property? – iiminov Feb 16 '18 at 09:37
  • Nevermind my previous question. I looked up package resource type and this is what I found. DriveItems with the package facet do not include a folder or file facet but are conceptually similar to an item with a folder facet. Which explains lack of mimeType and file extension. My guess is that notebooks are not for offline use. – iiminov Feb 16 '18 at 12:08
  • Vadim Gremyachev How can I get @microsoft.graph.downloadUrl for file version history files? – Abhishek Singh Jan 10 '20 at 04:22