使用 Microsoft Graph 将文件上传到 SharePoint 驱动器

dan*_*fer 6 sharepoint onedrive microsoft-graph-api

我们正在尝试使用 Microsoft Graph rest API 实现 Web 应用程序和 SharePoint Online 之间的集成。

具体来说,我们需要将文件上传到特定 SharePoint 站点的文档库(驱动器),与当前用户默认驱动器不同。我们通过 Azure AD 获取访问令牌,可以访问所有文件。

我们可以使用任何驱动器上传文件,/v1.0/me/drive/...但当我们使用另一个驱动器时则不能。

例如:

var response = client.PutAsync(graphResourceUrl +
    "/beta/sharepoint/sites/" + siteCollSiteId +
    "/lists/" + listId +
    "/drive/root/children/" + fileName + ":/content",
    new ByteArrayContent(fileBytes)).Result;

var response = client.PutAsync(graphResourceUrl +
    "/beta/drives/" + "/" + listId +
    "/items/" + siteCollSiteId + "/" + fileName + ":/content",
    new ByteArrayContent(fileBytes)).Result;

var response = client.PutAsync(graphResourceUrl +
    "/beta/drives/" + listId + "/" + fileName + ":/content",
    new ByteArrayContent(fileBytes)).Result;
Run Code Online (Sandbox Code Playgroud)

两者/v1.0/beta(在含杂的SharePoint路径的情况下)我们得到的错误响应Not Implemented

我们可能做错了什么?是否还不能与 Microsoft Graph 配合使用(除了/me)?

Bri*_*ith 9

为了使用 v1.0 获取驱动器的所有文件,您首先需要获取访问令牌(我看到您已经通过了该令牌),然后获取“驱动器 ID”并使用以下 URL(注意:它不是“驱动器”,而是“驱动器”):

https://graph.microsoft.com/v1.0/drives/{drive-id}/root/children
Run Code Online (Sandbox Code Playgroud)

为了获取驱动器 ID,我使用邮递员发出了以下 GET 请求,这将列出站点上的所有驱动器,您将能够获得该驱动器的 ID:

https://graph.microsoft.com/v1.0/sites/{tenant}.sharepoint.com:{path-to-site(ie: /sites/HR)}:/drives
Run Code Online (Sandbox Code Playgroud)

要回答有关上传文件的问题,您将向以下 URL 发出 PUT 请求:

https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{folder-name}/{file-name.txt}:/content
Run Code Online (Sandbox Code Playgroud)

您需要设置两个必需的标头:

  • 授权
  • 内容类型

接下来,您将文件的二进制流传递到请求正文中。

其他有用的物品

获取文件夹内的所有文件:

https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{folder-name}:/children
Run Code Online (Sandbox Code Playgroud)

获取用户 OneDrive 的内容:

https://graph.microsoft.com/v1.0/me/drive/root/children
Run Code Online (Sandbox Code Playgroud)

参考:https : //developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_put_content#example-upload-a-new-file


小智 1

:通常,:/content 我最好先获取 sp 库的driveId,然后使用 /v1.0/drive/{driveId}/ 在 v1.0 端点上工作