尝试使用 SharePoint 进行身份验证并使用访问令牌通过 API 获取文件列表

问题描述 投票:0回答:1

我正在尝试使用 API 遵循文档并从 SharePoint 读取文件。 我有一个 client_id+client_secret,我可以像这样检索访问令牌:

然后我尝试使用此令牌并调用

GetFolderByServerRelativeUrl
:

我收到 401(未经授权)错误:ID3035:请求无效或格式错误。

我并不完全熟悉 SharePoint,因此甚至不确定“AAA”是否被视为文件夹。这是一个文档库,我想从中访问“每月报告”文件夹的内容。

如有任何帮助,我们将不胜感激。

azure sharepoint microsoft-graph-api microsoft-sharepoint-api
1个回答
0
投票

我在 Finance

 SharePoint 网站中创建了一个名为 
AAA 的文档库,其中包含一个如下所示的文件夹:

enter image description here

我注册了一个 Entra ID 应用程序并授予了 Delegate 类型的 API 权限,如下所示:

enter image description here

现在,我通过 Postman 使用客户端凭据流生成访问令牌,如下所示:

POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
grant_type:client_credentials
client_id:appID
client_secret:secret
scope: https://tenant.sharepoint.com/.default

回复:

enter image description here

当我使用此令牌调用 SharePoint REST API 时,我也遇到了相同的错误,如下所示:

GET https://tenant.sharepoint.com/sites/Finance1/_api/web/GetFolderByServerRelativeUrl('AAA')

回复:

enter image description here

要解决此问题,请使用像授权代码一样的委托流程生成访问令牌,因为我们在应用程序注册中授予了委托类型的权限。

最初,我通过在浏览器中运行以下授权 URL 来获取

code
值:

https://login.microsoftonline.com/tenantID/oauth2/v2.0/authorize
?client_id=appID
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope=https://tenant.sharepoint.com/.default
&state=12345

enter image description here

现在,我使用此代码通过 Postman 生成具有授权代码流的访问令牌:

POST https://login.microsoftonline.com/tenantID/oauth2/v2.0/token
grant_type:authorization_code
client_id:appID
client_secret:secret
scope: https://tenant.sharepoint.com/.default
code:code
redirect_uri: https://jwt.ms

回复:

enter image description here

当我使用此令牌调用 SharePoint REST API 时,我成功获得了响应,如下所示:

GET https://tenant.sharepoint.com/sites/Finance1/_api/web/GetFolderByServerRelativeUrl('AAA')

回复:

enter image description here

要检索该文件夹中存在的文件,您可以使用以下 SharePoint REST API 调用:

GET https://tenant.sharepoint.com/sites/Finance/_api/web/GetFolderByServerRelativeUrl('AAA/Monthly Reports')/Files

回复:

enter image description here

您还可以使用 Microsoft Graph API 通过 REST API 下载 SharePoint 文件。

参考: azure - 使用 Rest API 从 Secure Sharepoint 下载文件 - AudienceUriValidationFailedException - Thinbug

© www.soinside.com 2019 - 2024. All rights reserved.