使用 Rest API 从 Secure Sharepoint 下载文件 - AudienceUriValidationFailedException

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

我一直在努力尝试从我们公司的安全 Sharepoint 下载安全文件。

我已执行以下步骤,但在尝试下载时仍然收到 AudienceUriValidationFailedException。

请参阅下面的我的流程。

我迫切需要一些指导。我错过了一个步骤吗?我是不是忘记了什么?使用了错误的客户端密钥?

  1. 我在 Azure portal

    上创建了应用程序注册
  2. 我在 registered app

    下创建了一个客户端密钥
  3. 我使用“https://[公司名称]-admin.sharepoint.com/_layouts/15/appinv.aspx”将应用程序添加到我的公司 SharePoint。我使用 Azure 应用程序注册中的“应用程序(客户端)ID”字符串。我单击“查找”按钮,然后出现“标题”字段 auto-populates.

  4. 我点击“创建”,然后

  5. 使用邮递员,我使用发布请求来获取access token.

  • 红色内容取自“Directory (tenat) ID”
  • 橙色来自“应用程序(客户端)ID”
  • 绿色来自我在步骤 2 中创建的秘密的“值”列
  1. 我使用它返回的令牌来下载文件from the SharePoint
azure microsoft-graph-api sharepoint-online sharepoint-rest-api
2个回答
1
投票

当您为

Microsoft graph
生成访问令牌并将其用于运行 SharePoint 请求时,出现错误 AudienceUriValidationFailedException

要解决该错误,请使用以下Graph API查询从站点下载文件:

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

我的 SharePoint 网站中有一个文档库,其中包含

logo.jpg
文件,如下所示:

enter image description here

要使用不记名令牌通过 REST API 下载此文件,我注册了一个 Azure AD 应用程序并添加了 API permissions

,如下所示:

enter image description here

现在我通过 Postman 使用以下参数生成了

访问令牌

POST https://login.microsoftonline.com/tenantID/oauth2/v2.0/token client_id:appID client_secret:secret scope: https://graph.microsoft.com/.default grant_type:client_credentials

回复:

enter image description here

我在运行下面的图形查询时使用了这个令牌,并在

response 中获得了

logo.jpg 的文件下载链接,如下所示:

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

回复:

enter image description here

当我从浏览器中的响应中运行

downloadUrl时,文件下载成功,如下所示:

enter image description here

您可以使用下面的图形查询来获取您网站的 ID:

GET https://graph.microsoft.com/v1.0/sites/root:/sites/<sitename>

回复:

enter image description here

同样,您可以使用以下查询来获取文档库的

驱动器ID

GET https://graph.microsoft.com/v1.0/sites/<siteID_from_above_response>/drives

回复:

enter image description here


0
投票
如果您要使用 Sharepoint REST API,请在获取令牌步骤中使用范围

https://<tenantname>.sharepoint.com/.default

。这应该可以解决当前配置的问题,并且您将能够使用 Sharepoint REST API (
docs)。

但是,如果您想使用 Graph API(其中范围是您在步骤 5 中的屏幕截图中的范围)(

docs),您应该添加到在 Azure AD > API 权限 > 应用程序中注册的应用程序,而不是步骤 3,4 > Microsoft Graph API > 选择您可能需要的特定权限(user.read, mail.read, file.readwrite

 等)

您也可以使用委托级别的权限,但在这种情况下获取访问令牌的方式不同,而不是

client_credentials


    

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