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

时间:2018-08-29 12:10:43

标签: microsoft-graph 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 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”,“不支持的细分类型...”

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

有人知道吗?

1 个答案:

答案 0 :(得分:0)

我检查了是否可以将文件上传到属于另一个用户的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或共享项的集合。它不允许创建新项目。