将项目上传到个人 OneDrive 共享文件夹

Uploading items to a personal OneDrive shared folder

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 docs for upload 我试过了

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"、"Unsupported segment type..."

编辑:我现在已经在 OneDrive Web UI 中为 OneDrive 用户 A 和 B 使用两个不同的浏览器来解决这个问题,所以我知道这是可能的(无需先将共享文件夹添加到 B 自己的根目录) ),但我需要一些帮助来确定对 OneDrive REST 的正确请求 API。

有人知道吗?

我检查了将文件上传到 OneDrive 上属于另一个用户的共享文件夹的可能性。使用 GraphExplorer.

实现它没有任何问题

这是我所做的:

  1. 我得到了共享文件和文件夹的列表:

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>"
                        }
                    }
                }
            }
        }
    ]
}
  1. 然后我使用以下数据执行 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 requests 访问共享的 DriveItem 或共享项目的集合。它不允许创建新项目。