OneDrive API(REST),复制不同OneDrive帐户之间的文件/文件夹

时间:2016-03-15 16:54:29

标签: rest fiddler onedrive

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

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

  

复制操作的目标必须是文件夹。文件夹本身无法复制。此外,OneDrive存储的结构使得不同用户的OneDrive文件夹层次结构之间不会发生复制操作。例如,即使用户A可以读取用户B的文件,用户A也无法将其复制到自己的OneDrive文件夹中。

我没有使用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"}
}

enter image description here

当我想通过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"}
}

enter image description here

错误回复:

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

我使用的范围是:

  • onedrive.readwrite
  • wl.signin
  • wl.skydrive
  • wl.skydrive_update
  • wl.contacts_skydrive

1 个答案:

答案 0 :(得分:0)

我想我已经找到了答案。

当帐户A与帐户B共享文件夹时,共享文件夹确实显示在"共享"在账户B上!

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

enter image description here

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

enter image description here

如果有人有兴趣通过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);