API Sharepoint:使用 App Azure 列出和下载文件

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

我需要使用 API 连接到 Sharepoint,从 API 下载文件列表,然后下载它。

我尝试在 Microsoft Entra 中创建应用程序并通过“sites.fullcontrol.all”为其授予所有权限,但它不起作用。

当我使用

GetFolderByServerRelativePath()
时,我得到这样的回复:

{"error_description":"Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown."}

我不确定要使用哪个端点。

有人可以告诉我使用哪个端点吗?

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

如果出现以下情况,通常会出现错误

AudienceUriValidationFailedException
您正在生成具有 Microsoft Graph 范围的令牌并将其用于 SharePoint API。

最初,当我使用 GetFolderByServerRelativePath() 以及使用 MS Graph 范围生成的令牌时,我也遇到了 同样的错误

GET http://xxxxxxxxx.sharepoint.com/sites/site_name/_api/web/GetFolderByServerRelativePath(decodedUrl='Shared Documents/Test')

回复:

enter image description here

解决错误,您需要使用 SharePoint 范围 (https://yourtenant.sharepoint.com/.default) 生成访问令牌或切换到 Graph API 调用。

就我而言,我注册了一个 Azure AD 应用程序并授予应用程序类型的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://graph.microsoft.com/.default

回复:

enter image description here

您可以使用此令牌调用下面的 Graph API 查询,该查询会列出文件以及下载 URL:

GET https://graph.microsoft.com/v1.0/sites/<siteID>/drives/<doclib driveID>/root/children/

回复:

enter image description here

现在,单击响应中的下载 URL,文件将成功下载,如下所示:

enter image description here

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

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