I have 2 OneDrive Accounts. Account A shares a Folder with Account B. I am logged in with Account B and want to copy the shared Folder of Account A to a Folder in Account B via REST.
I know the OneDrive Live SDK Documentation says: 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.
I am not using the Live API https://apis.live.net/v5.0/
but the OneDrive API https://api.onedrive.com/v1.0/
When I copy Folder to Folder in my Own OneDrive, everything is fine:
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"}
}
When I want to access the Folder of Account A via REST in Fiddler I get te following Error.
Rest Call:
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 Response:
{"error":{"code":"itemNotFound","message":"Item does not exist"}}
The Scopes I use are:
I think I already found the Answer.
When Account A shares a Folder with Account B the Shared Folder does appear under "Shared" on Account B!
So I just have to get the ID of this Folder in "Shared" to copy it into my preferred target Folder on Account 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"}
}
If someone is interested to perform this REST CALL via C#, just use RestSharp http://restsharp.org/. C# Code Snippet below
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);