You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用获取SharePoint列表项附件遇401问题求助

问题描述

我正在开发一款Flutter应用,需要获取列表项数据及对应附件(若存在)。目前使用Graph API:

https://graph.microsoft.com/v1.0/sites/$siteId/lists/$targetListId/items?expand=fields

获取所有列表项,当hasAttachments字段为true时尝试获取附件。我以为没有对应的Graph API,于是尝试使用以下SharePoint API代码:

final String graphEndpoint =
        'https://mytenant.sharepoint.com/_api/web/lists/getbytitle(Disposizioni di servizio))/items({$itemId})?\$expand=AttachmentFiles';

但始终返回401错误。我使用aad_oauth Flutter库通过应用内WebView获取访问令牌,令牌包含以下权限范围:

opened 
profile 
offline_access 
Sites.Read.All 
https://mytenant.sharepoint.com/AllSites.Read

请问我哪里操作有误?是否需要不同的访问令牌?如何获取?

解决方案

1. 纠正误区:Graph API支持直接获取列表项附件

你不需要切换到SharePoint REST API,Graph API本身就提供了获取列表项附件的接口:

  • 单独获取某列表项的附件:
https://graph.microsoft.com/v1.0/sites/$siteId/lists/$targetListId/items/$itemId/attachments
  • 获取列表项时同时扩展附件字段:
https://graph.microsoft.com/v1.0/sites/$siteId/lists/$targetListId/items?expand=fields,attachments

这样可以一次性拿到带附件的列表项数据,你的Sites.Read.All权限已经足够调用这个接口,无需额外配置。

2. SharePoint API请求的语法错误(若仍需使用)

如果坚持用SharePoint REST API,你的请求URL有两处语法错误:

  • getbytitle的参数需要用单引号包裹,正确写法是getbytitle('Disposizioni di servizio')
  • URL末尾多了一个右括号,需删除多余的)
    修正后的URL:
final String graphEndpoint =
        'https://mytenant.sharepoint.com/_api/web/lists/getbytitle(\'Disposizioni di servizio\')/items({$itemId})?\$expand=AttachmentFiles';

3. 访问令牌的受众问题

Graph API和SharePoint REST API的令牌受众不同:

  • Graph API令牌的受众是https://graph.microsoft.com
  • SharePoint REST API令牌的受众是你的租户SharePoint站点(如https://mytenant.sharepoint.com)

你当前获取的令牌是针对Graph API的,所以用于SharePoint REST API会返回401。如果要调用SharePoint REST API,需要在请求令牌时指定受众为你的SharePoint站点地址,确保权限范围匹配。但更推荐用Graph API的附件接口,避免令牌切换的麻烦。

内容的提问来源于stack exchange,提问作者Nicola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 20:52:12