OneDrive API (REST),在不同的 OneDrive 帐户之间复制 Files/Folder

OneDrive API (REST), Copy Files/Folder between different OneDrive Accounts

我有 2 个 OneDrive 帐户。 账户 A 与账户 B 共享一个文件夹。 我使用账户 B 登录,并希望通过 REST 将账户 A 的共享文件夹复制到账户 B 中的文件夹。

我知道 OneDrive Live SDK 文档说: https://msdn.microsoft.com/en-us/library/dn659743.aspx

The destination of a copy operation must be a folder. Folders themselves cannot be copied. Also, OneDrive storage is structured so that copy operations cannot occur between the OneDrive folder hierarchies of different users. For example, even if user A can read user B's files, user A cannot copy them to his or her own OneDrive folders.

我没有使用 Live API https://apis.live.net/v5.0/ 但 OneDrive API https://api.onedrive.com/v1.0/

当我将文件夹复制到我自己的 OneDrive 中的文件夹时,一切正常:

POST https://api.onedrive.com/v1.0/drive/root:/test:/action.copy

Authorization: Bearer {ACCESS TOKEN}
Content-Type: application/json
Prefer: respond-async

{
 "parentReference" : {"path": "/drive/root:/target"}
}

当我想在 Fiddler 中通过 REST 访问帐户 A 的文件夹时,出现以下错误。

休息电话:

POST https://api.onedrive.com/v1.0/drives/38A2C8D42D476A18/root:/test:/action.copy

Authorization: Bearer {ACCESS TOKEN}
Content-Type: application/json
Prefer: respond-async

{
 "parentReference" : {"path": "/drive/root:/target"}
}

错误响应:

{"error":{"code":"itemNotFound","message":"Item does not exist"}}

我使用的范围是:

我想我已经找到了答案。

当账户 A 与账户 B 共享文件夹时,共享文件夹确实出现在账户 B 的 "Shared" 下!

所以我只需要在 "Shared" 中获取此文件夹的 ID,然后将其复制到我在帐户 B 上的首选目标文件夹中。

POST https://api.onedrive.com/v1.0/drive/items/FOLDER-ID/action.copy

Authorization: Bearer {ACCESS TOKEN}
Content-Type: application/json
Prefer: respond-async

{
 "parentReference" : {"path": "/drive/root:/target"}
}

如果有人有兴趣通过 C# 执行此 REST CALL,只需使用 RestSharp http://restsharp.org/。下面的 C# 代码片段

RestRequest restRequest = new RestRequest(Method.POST);
restRequest.AddHeader("Authorization", "Bearer " + accessToken);
restRequest.AddHeader("Prefer", "respond-async");
restRequest.AddParameter("application/json; charset=utf-8", "{\"parentReference\": {\"path\": \"/drive/root:/target\"}}", ParameterType.RequestBody);
RestClient client = new RestClient("https://api.onedrive.com/v1.0/drive/items/FOLDER-ID/action.copy");
IRestResponse _response = client.Execute(restRequest);