Search code examples
c#jsononedrive

Download shared document with OneDrive API


I'm trying to download a Word document which is shared with a user using the OneDrive API.

I used the documentation from this page. First I'm getting a list of shared files using the following code:

using (HttpClient client = new HttpClient())
{    
    client.BaseAddress = new Uri("https://api.onedrive.com");
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
    <ACCESS TOKEN>);

    HttpResponseMessage response = await client.GetAsync("/v1.0/drive/oneDrive.sharedWithMe");
}

As the documentation says I'm getting a driveItem resource. Documentation can be found here. The response is this JSON:

{  
   "value":[  
      {  
         "@content.downloadUrl":"DOWNLOAD URL",
         "createdDateTime":"2017-03-09T09:08:19.227Z",
         "cTag":"CTAG",
         "eTag":"ETAG",
         "id":"ID",
         "lastModifiedDateTime":"2017-03-09T13:39:42.133Z",
         "name":"Word document.docx",
         "webUrl":"WEBURL",
         "remoteItem":{  
            "createdBy":{  
               "user":{  
                  "displayName":"username",
                  "id":"USERID"
               }
            },
            "fileSystemInfo":{  
               "createdDateTime":"2017-03-09T08:50:52.663Z",
               "lastModifiedDateTime":"2017-03-09T13:06:19.653Z"
            },
            "file":{  
               "hashes":{  
                  "sha1Hash":"23894BC4D8E941FFA861851F9B47DCBD9DB061C6"
               },
               "mimeType":"image/png"
            },
            "id":"REMOTE ID",
            "lastModifiedBy":{  
               "user":{  
                  "displayName":"username",
                  "id":"USER ID"
               }
            },
            "lastModifiedDateTime":"2017-03-09T13:39:28.497Z",
            "name":"Word document.docx",
            "parentReference":{  
               "driveId":"DRIVE ID"
            },
            "shared":{  
               "owner":{  
                  "user":{  
                     "displayName":"username",
                     "id":"DRIVE ID"
                  }
               }
            },
            "size":15854,
            "webUrl":"WEBURL"
         }
      }
   ]
}

Im using the downloadUrl from this JSON to download the file. The URL works so I can download the file and at first it looks like it's a word document. But it is actually a png file. I expect the mime type to be application/vnd.openxmlformats-officedocument.wordprocessingml.document or any other mime type that belongs to .docx documents. but the JSON says the mime type is image/png. So I can't open the file using Word but I have to open it as an image. When opening as a image I can see the content of the file but i want the actual Word document.

I tried downloading the file in bytes and convert it to a Word document. No luck of course because the downloaded file is really a png...

How can I download the shared file as the document type I expect it to be?


Solution

  • This looks like a bug in the service whereby it's somehow returning the thumbnail instead of the content. While we investigate and resolve that you could work around it by pulling the driveId value out of remoteItem.parentReference, and the id value out of remoteItem and crafting a request like:

    https://api.onedrive.com/v1.0/drives/driveid/items/id/content

    This will redirect you to a download URL that will give you what you want.

    I'll circle back and update this answer when the issue has been resolved.