我想构建与 SharePoint 平台的集成并使用提供的 API。
目标是获取网站中每个文档的内容。
我创建了一个应用程序以便将其用于 OAuth2。
该应用程序具有以下权限:
所有站点.阅读
所有站点.写入
到目前为止我所做的步骤:
我正在尝试使用以下 URL 获取所有网站
网址:https://graph.microsoft.com/v1.0/sites?search=%5C\*
获取站点 ID 后,我想获取驱动器,所以我使用了
网址:https://graph.microsoft.com/v1.0/sites/xxxxx.sharepoint.com/drives/
然后我使用驱动器 ID 来获取驱动器中的所有项目:
- 网址:https://graph.microsoft.com/v1.0/sites/xxxxx.sharepoint.com/drives/%7Bdrive-id%7D/items/root/children
- 但是这个端点提供了一个下载文件的url,我只需要读取内容。
我也这样做过:
在实现过程中我还尝试获取xxxxx.sharepoint.com站点列表中每个文档的内容。(以下URL中的列表id和项目id有效)
- 网址 https://graph.microsoft.com/v1.0/sites/xxxxx.sharepoint.com/lists/%7Blist-id%7D/items/%7Bitem-id%7D/driveItem/content
上面返回一个二进制响应,我无法读取文档的实际内容。
我尝试使用 iso-8859-1 和 utf-8 解码内容,但无法读取文件的内容。
我可以使用任何端点来读取文件的内容吗?
我还在微软门户上开启了讨论。
https://learn.microsoft.com/en-us/answers/questions/1651755/read-the-content-of-a-sharepoint-file-using-the-ap?comment=question
该端点将以二进制格式返回文件的内容。您需要根据文件类型适当地处理此二进制数据。例如,如果它是文本文件,您可以使用适当的编码将二进制数据解码为字符串。
这是使用 HttpClient 获取文件内容的 C# 基本示例:
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string siteId = "{site-id}";
string driveId = "{drive-id}";
string itemId = "{item-id}";
string accessToken = "your-access-token";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
HttpResponseMessage response = await client.GetAsync($"https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{driveId}/items/{itemId}/content");
if (response.IsSuccessStatusCode)
{
byte[] content = await response.Content.ReadAsByteArrayAsync();
// Process the content here, e.g., convert to string for text files
string fileContent = System.Text.Encoding.UTF8.GetString(content);
Console.WriteLine(fileContent);
}
else
{
Console.WriteLine($"Failed to fetch file content. Status code: {response.StatusCode}");
}
}
}
}