Microsoft Graph: Uploading files to shared library

2019-02-16 05:36发布

问题:

In the documentation about Upload, these request options are listed:

PUT /me/drive/items/{parent-id}:/{filename}:/content
PUT /me/drive/root:/{parent-path}/{filename}:/content
PUT /me/drive/items/{parent-id}/children/{filename}/content
PUT /groups/<id>/drive/items/<parent-id>/children/<filename>/content

In the documentation about Resumable Uploads, Create an upload session show these options:

POST /me/drive/root:/{path-to-item}:/createUploadSession
POST /me/drive/items/{parent-item-id}:/{filename}:/createUploadSession

What if I have to upload to other (not 'me') drive? For example, the default shared library (https://mycompany.sharepoint.com/Shared%20Documents), which for any other porpoise besides uploading, the documentation says can be accessed like this:

/drives/{drive-id}/items/{item-id}

So, my question is: Is it possible to upload to "/Shared Documents"? If so, which is the right syntax for the PUT (small file) or POST (upload session)? Perhaps something similar to this? (I just made this up, and it doesn't work)

PUT /drives/{drive-id}/items/{parent-id}/{filename}:/content

or (in case of an upload session):

POST /drives/{drive-id}/items/{parent-item-id}:/{filename}:/createUploadSession

For example, in the Graph Explorer, the response for something like this:

/v1.0/drives/THEDRIVEID/items/THEFOLDERID:/whatever.jpg:/createUploadSession

was:

{
    "error": {
        "code": "invalidRequest",
        "message": "A valid path must be provided.",
        "innerError": {
            "request-id": "THERETURNEDID",
            "date": "THERETURNEDDATE"
        }
    }
}

EDIT: Because the company's root can also be accessed as /drive/root/, I also tried (with no luck, in both 1.0 and beta):

/drive/root:/whatever.jpg:/createUploadSession
/drive/root/whatever.jpg:/createUploadSession

回答1:

This is possible, but you need the right app permissions to be able to read/create files outside of the user's OneDrive. Instead of requesting Files.ReadWrite, you would need to request Sites.ReadWrite.All and then use /v1.0/drive/root:/file.bin:/createUploadSession.

You can also use the SharePoint Sites API in Microsoft Graph to access team site document libraries (other than the root site). However, this API is still in the beta version and should not be used in production apps.