使用 Microsoft Graph 将文件上传到 SharePoint 驱动器 [英] Upload file to SharePoint drive using Microsoft Graph
问题描述
我们正在尝试使用 Microsoft Graph rest API 实现 Web 应用程序和 SharePoint Online 之间的集成.
We are trying to implement integration between a web application and SharePoint Online using Microsoft Graph rest API.
具体来说,我们需要将文件上传到特定 SharePoint 网站的文档库(驱动器),不同于当前用户的默认驱动器.我们通过 Azure AD 获取访问令牌,可以访问所有文件.
Specifically, we need to upload a file to a specific SharePoint site's document library (drive), different than current user default drive. We get the access token through Azure AD with access to all files.
我们可以使用 /v1.0/me/drive/...
将文件上传到任何驱动器,但当我们使用另一个驱动器时则不行.
We can upload files into any drive using /v1.0/me/drive/...
but not when we use another 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;
/v1.0
和 /beta
(在 SharePoint 包含路径的情况下)我们都收到 Not Implemented
的错误响应.
Both /v1.0
and /beta
(in the case of SharePoint containing path) we are getting an error response of Not Implemented
.
我们可能做错了什么?它是否还不能与 Microsoft Graph 一起使用(/me
除外)?
What could we be doing wrong? Is it not yet working with Microsoft Graph (other than /me
)?
推荐答案
为了使用 v1.0 获取驱动器的所有文件,您首先需要获取访问令牌(我看到您已经通过了),然后获取驱动器 ID"并使用以下 URL(注意:它不是驱动器",而是驱动器"):
In order to get all the files of a drive using v1.0, you would first need to get an access token (which I see you are already passed that), then get the 'drive-id' and use the following URL(note: its not 'drive' it is 'drives'):
https://graph.microsoft.com/v1.0/drives/{drive-id}/root/children
为了获取驱动器 ID,我使用 postman 发出以下 GET 请求,这将列出站点上的所有驱动器,您将能够获取该驱动器的 ID:
To get the drive id, I made the following GET request using postman, this will list all the drives on the site and you will be able to get the ID of that drive:
https://graph.microsoft.com/v1.0/sites/{tenant}.sharepoint.com:{path-to-site(ie: /sites/HR)}:/drives
要回答有关上传文件的问题,您将向以下 URL 发出 PUT 请求:
To answer your question regarding Uploading files, you will make a PUT request to the following URL:
https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{folder-name}/{file-name.txt}:/content
您需要设置两个必需的标题:
You will need to set two required headers:
- 授权
- 内容类型
接下来,您将文件的二进制流传递到请求正文中.
Next, you will pass the binary stream of the file into the body of the request.
其他有用的项目
获取文件夹内的所有文件:
Get all files inside of a folder:
https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{folder-name}:/children
获取用户 OneDrive 的内容:
Get content of users OneDrive:
https://graph.microsoft.com/v1.0/me/drive/root/children
这篇关于使用 Microsoft Graph 将文件上传到 SharePoint 驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!