'OneDrive REST APIs: "message": "The specified item does not have content." when downloading a file

the use case

I would like to download an excel sheet from OneDrive using the REST API

Documentation:

the documentation proposed to call this Endpoint : GET /me/drive/items/{item-id}/content

prerequisites

  1. I am correctly logged using oauth2, all other methods work such as '/me', '/me/drive/recent', '/me/drive/sharedWithMe'

  2. The granted credentials on

The error message when I call GET /me/drive/items/{item-id}/content

{
  "error": {
    "code": "itemNotFound", 
    "innerError": {
      "client-request-id": "aaaaaaa-aaaa-aaaa-aaaa-aaaaa", 
      "date": "2020-09-26T09:19:08", 
      "request-id": "aaaaa-aaaa-aaaa-aaa-aaa"
    }, 
    "message": "The specified item does not have content."
  }
}

The file exists and contains data when I call GET /me/drive/items/{item-id}

The file exists and weight : 46.272 bytes



{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('<a_username>%40hotmail.com')/drive/items/$entity", 
  "createdBy": {
    "user": {
      "displayName": "<a_name> ", 
      "id": "<a_drive_id>"
    }
  }, 
  "createdDateTime": "2020-09-27T07:29:04.893Z", 
  "eTag": "<an_eTag>", 
  "fileSystemInfo": {
    "lastAccessedDateTime": "2020-09-27T11:52:31Z"
  }, 
  "id": "<a_drive_id>!<a_sub_id>", 
  "lastModifiedBy": {
    "user": {
      "displayName": "<a_name> ", 
      "id": "<a_drive_id>"
    }
  }, 
  "lastModifiedDateTime": "2020-09-27T18:46:25.073Z", 
  "name": "a_filename.xlsx", 
  "parentReference": {
    "driveId": "<a_drive_id>", 
    "driveType": "personal"
  }, 
  "remoteItem": {
    "file": {}, 
    "fileSystemInfo": {
      "lastAccessedDateTime": "2020-09-27T11:52:31Z"
    }, 
    "id": "<an_id>!7757", 
    "name": "a_filename.xlsx", 
    "parentReference": {
      "driveId": "<an_id>", 
      "driveType": "personal"
    }, 
    "size": 46272, 
    "webUrl": "https://1drv.ms/u/<a_letter>!<a_web_url_id>"
  }, 
  "webUrl": "https://1drv.ms/u/<a_letter>!<a_web_url_id>"
}

Conclusion

Is there a bug in the implementation of the API by Microsoft ?!?

What do I do wrong?



Solution 1:[1]

Firstly, we need to notice that in the api document, it mentioned:

Only driveItems with the file property can be downloaded.

So we need to make sure the target item has the propertiy file in the response when we called https://graph.microsoft.com/v1.0/me/drive/items/item_id

enter image description here

Then we need to pay attention to this section, when we can call the download api successfully, it will return a 302 redirection

enter image description here

Solution 2:[2]

Not sure if this is your situation, but make sure you have checked-in your file if you happened to have uploaded it programmatically. OneDrive/SharePoint likes to put files into no-man's land if it has not been properly checked-in.

Documentation:

Download: https://docs.microsoft.com/en-us/graph/api/driveitem-checkout?view=graph-rest-1.0&tabs=http

Checkin: https://docs.microsoft.com/en-us/graph/api/driveitem-checkin?view=graph-rest-1.0&tabs=http

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tiny Wang
Solution 2 Nelson Nyland