microsoft-graph-api - 将项目上传到个人 OneDrive 共享文件夹
问题描述
OneDrive 用户 A 与 OneDrive 用户 B 共享一个文件夹,B 可以使用共享 ID 访问该文件夹。例如使用图形浏览器
GET https://graph.microsoft.com/v1.0/shares/{shareId}
产量
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#shares/$entity",
"id": "{shareId}",
"name": "ASharedFolder",
"owner": { ... }
}
现在,B 想将一个新文件上传到 ASharedFolder。
阅读我尝试过的上传 OneDrive 文档
PUT https://graph.microsoft.com/v1.0/shares/{shareId}/driveItem/children:/SomeFile.txt:/content
Content-Type text/plain
some text goes here
也
PUT https://graph.microsoft.com/v1.0/shares/{shareId}/items/{sharedItemId}:/SomeFile.txt:/content
Content-Type text/plain
some text goes here
但两者都产生“BadRequest”、“不支持的段类型......”
编辑:我现在已经在OneDrive Web UI中为 OneDrive 用户 A 和 B 使用两种不同的浏览器播放了这个场景,所以我知道这是可能的(无需先将共享文件夹添加到 B 自己的根目录),但我需要一些帮助找出对 OneDrive REST API 的正确请求。
有人知道吗?
解决方案
我检查了将文件上传到 OneDrive 上属于另一个用户的共享文件夹的可能性。使用GraphExplorer实现它没有任何问题。
这是我所做的:
- 我得到了共享文件和文件夹的列表:
GET /me/drive/sharedWithMe
返回(部分数据已省略):
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)",
"value": [
{
"@odata.type": "#microsoft.graph.driveItem",
"id": "<itemId>",
"name": "Folder name",
"parentReference": {
"driveId": "<myUserId>",
"driveType": "personal"
},
"remoteItem": {
"id": "<remoteItemId>",
"name": "Folder name",
"createdBy":
"user": {
"displayName": "Other user name",
"id": "<otherUserId>"
}
},
"folder": {
"childCount": 0
},
"parentReference": {
"driveId": "<otherUserId>",
"driveType": "personal"
},
"shared": {
"owner": {
"user": {
"displayName": "Other user name",
"id": "<otherUserId>"
}
}
}
}
}
]
}
- 然后我使用以下数据执行 PUT 请求:
PUT /drives/{otherUserId}/items/{remoteItemId}:/test.txt:/content
Content-Type: text/plain
The contents of the file goes here.
回复:Success - Status Code 201
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('<otherUserId>')/items/$entity",
"id": "<itemId>",
"name": "test.txt",
"size": 35,
"createdBy": {
"application": {
"displayName": "Graph explorer"
},
"user": {
"displayName": "My user name",
"id": "<myUserId>"
}
},
"parentReference": {
"driveId": "<otherUserId>",
"driveType": "personal",
"id": "<parentReferenceId>",
"name": "Folder name",
"path": "/drives/<otherUserId>/items/<parentReferenceId>"
},
"file": {
"mimeType": "text/plain"
}
}
然后,在随后的GET /me/drive/sharedWithMe
请求中,该文件夹的childCount的值已增加到 1。
笔记:
\shares
端点只允许GET 请求访问共享的 DriveItem 或共享项目的集合。它不允许创建新项目。
推荐阅读
- java - Spring:依赖库中的自动配置 bean 未在 Spring 上下文中加载
- vaadin - 如何删除 vaadin Grid 多选列中的标题
- c# - Visual Studio 假定 FirstOrDefaultAsync() 的返回值不能为空
- c++ - 地图中索引的最大输入大小是多少?
- visual-studio-code - 让 VSCode 按子文件夹进行不同的格式设置
- android - 没有为 workmanager 调用 onStopped()
- solr - 在 Solr 中使用嵌套字段更新现有文档
- html - 字符串中的 HTML 变量
- sql - SQL 中的期初余额和期末余额(按不同列分组)
- windows - 在操作系统范围内用括号封装文本