gpt4 book ai didi

c# - 使用 OneDrive API 下载共享文档

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:58 26 4
gpt4 key购买 nike

我正在尝试下载使用 OneDrive API 与用户共享的 Word 文档。

我使用了来自 this 的文档页。首先,我使用以下代码获取共享文件列表:

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");
}

正如文档所说,我正在获取 driveItem 资源。可以找到文档 here .响应是这个 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"
}
}
]
}

我正在使用此 JSON 中的 downloadUrl 下载文件。该 URL 有效,因此我可以下载该文件,起初它看起来像是一个 word 文档。但它实际上是一个png文件。我希望 mime 类型为 application/vnd.openxmlformats-officedocument.wordprocessingml.document 或属于 .docx 文档的任何其他 mime 类型。但是 JSON 表示 mime 类型是 image/png。所以我无法使用 Word 打开文件,但我必须将其作为图像打开。当以图像形式打开时,我可以看到文件的内容,但我想要实际的 Word 文档。

我尝试以字节为单位下载文件并将其转换为 Word 文档。当然不走运,因为下载的文件实际上是 png...

如何将共享文件下载为我期望的文档类型?

最佳答案

这看起来像是服务中的错误,它以某种方式返回缩略图而不是内容。虽然我们调查并解决了您可以通过从 remoteItem.parentReference 中提取 driveId 值并将 id 值从 remoteItem 并制作如下请求:

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

这会将您重定向到一个下载 URL,该 URL 将为您提供您想要的内容。

问题解决后,我会返回并更新此答案。

关于c# - 使用 OneDrive API 下载共享文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com