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

Azure Synapse Web Activity调用SharePoint API遇未授权访问问题求助

1. Web活动调用SharePoint REST API未授权错误排查方案
  • 检查令牌权限范围:获取访问令牌时,请求的scope必须包含https://testsite.sharepoint.com/.default,且对应的Azure AD应用已配置Sites.Read.All或Sites.ReadWrite.All的应用权限(注意是应用权限,而非委派权限,因为Synapse管道为后台运行)。
  • 验证令牌有效性:通过https://jwt.ms解码令牌,确认aud(受众)匹配你的SharePoint站点域名,roles字段包含已配置的权限。
  • 核对请求头配置:Web2活动必须在请求头中添加Authorization: Bearer <获取到的令牌>,同时添加Accept: application/json;odata=verbose或Accept: application/json,避免格式不兼容导致的权限校验失败。
  • 确认文件夹路径正确性:检查GetFolderByServerRelativeUrl中的路径是否与实际一致,比如/sites/Repository/Shared Documents/General(空格可直接保留或用%20转义,需保证与站点实际路径完全匹配)。
  • 检查权限授予状态:在Azure AD门户中,需对应用配置的权限点击「授予管理员同意」,否则权限不会生效。
2. Azure Synapse获取SharePoint文件夹文件列表的其他方法
  • 使用SharePoint Online连接器(Copy Data活动):
    虽然SharePoint Online List活动仅能查看库和列表,但可通过Copy Data活动结合SharePoint Online连接器实现子文件夹文件列表获取:

    • 新建Copy Data活动,源选择SharePoint Online连接器,连接类型选「文件系统」。
    • 配置站点URL、文档库路径及目标子文件夹路径,勾选「列出文件」选项。
    • 后续可通过Lookup活动或脚本活动提取文件列表的详细信息。
  • Synapse Notebook(Python/Spark):
    通过代码调用Microsoft Graph API或SharePoint REST API,灵活性更高,示例Python代码如下:

    import msal
    import requests
    
    # 配置参数
    client_id = "你的Azure AD应用ID"
    client_secret = "你的应用密钥"
    tenant_id = "你的租户ID"
    site_url = "https://testsite.sharepoint.com/sites/Repository"
    folder_path = "/sites/Repository/Shared Documents/General"
    
    # 获取访问令牌
    authority = f"https://login.microsoftonline.com/{tenant_id}"
    scope = [f"{site_url}/.default"]
    app = msal.ConfidentialClientApplication(client_id, authority=authority, client_credential=client_secret)
    result = app.acquire_token_for_client(scopes=scope)
    access_token = result["access_token"]
    
    # 调用API获取文件列表
    api_url = f"{site_url}/_api/web/GetFolderByServerRelativeUrl('{folder_path}')/Files"
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Accept": "application/json;odata=verbose"
    }
    response = requests.get(api_url, headers=headers)
    files = response.json()["d"]["results"]
    
    # 输出文件信息
    for file in files:
        print(f"文件名: {file['Name']}, 路径: {file['ServerRelativeUrl']}, 大小: {file['Length']}")
    
  • 集成Azure Logic Apps:
    若需要递归遍历子文件夹等复杂逻辑,可在Synapse管道中调用Azure Logic Apps。Logic Apps提供「列出文件夹中的文件」操作,支持递归遍历,可将结果返回给Synapse管道进一步处理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 07:20:41