Can not copy shared with me file to my own drive via OneDrive API

Ivan Banha 1 Reputation point
2021-07-23T08:09:11.2+00:00

In a service, I'm developing, I have a situation when user_a shared a file to user_b. Now, user_b has access to the file, and he wants to save a copy of the file to his drive.

I have found documentation about drive item copy function, but it's not clear to me how to use it. I have access tokens for both accounts.

First, I get the ID of the shared file from user_a (using tokens related to user_a). Second, using previous file ID, I'm calling copy endpoint with tokens of user_b. So, I have tried several calls.

  1. https://graph.microsoft.com/v1.0/me/drive/items/user_a_file_id/copy
    user_a_file_id - an ID of the file from user_a drive.
    An error I receive:
    "status": 404, "code": "itemNotFound", "message": "Item does not exist"

  1. https://graph.microsoft.com/v1.0/drives/user_a_drive_id/items/user_a_file_id/copy
    user_a_drive_id - an ID of a drive where shared file should be located. An error I receive:

    "status": 500, "code": "generalException", "message": "General Exception While Processing"

  2. https://graph.microsoft.com/v1.0/users/user_a_id/drive/items/user_a_file_id/copy
    user_a_id - an ID of the shared file owner. An error I receive:
    "status": 404, "code": "itemNotFound", "message": "Item does not exist"

I have all permissions in my app. But it does not found the item for some reason. Does anyone know how to use these APIs? Could you give me an example? Thanks in advance!

Microsoft Security | Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,396 Reputation points
    2021-08-06T09:34:00.273+00:00

    First , you cannot use /me endpoint to copy shared files, because user_a_file_id is not under user_b items, so the first api is not suitable for you. After my test, the second and third api can copy the shared files to my own drive correctly. Before that, you must ensure that the file is shared by user_a to user_b.

    Then, I suggest that you can directly use graph-explorer to call the second or third api, which requires you to log in to graph-explorer using user_b.

    POST https://graph.microsoft.com/v1.0/users/user_a_id/drive/items/user_a_file_id/copy  
      
    {  
    "parentReference": {  
    "driveId": "{user_b drive id}",  
    "id": "{Your shared files are stored in the folder id of uesr_b}"  
         }  
    }  
    

    121181-231.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.