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

使用Python3.6上传大文件至SharePoint时遇403权限错误求助

SharePoint创建文件夹时403权限拒绝问题排查与解决

问题场景

尝试使用Python3.6向SharePoint上传200MB大文件,在创建文件夹阶段触发403权限错误,怀疑是路径问题。

错误信息

office365.runtime.client_request_exception.ClientRequestException: ('-2147024891, System.UnauthorizedAccessException', 'Access denied.', "403 Client Error: Forbidden for url: https://xyz.sharepoint.com/_api/Web/folders/Add('Testing%20')")

代码片段

from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext

def get_sharepoint_context_using_user():
    username='xyz@abc.com'
    password='password'
    # Get sharepoint credentials
    sharepoint_url = 'https://abx.sharepoint.com'

    # Initialize the client credentials
    user_credentials = UserCredential(username,password)

    # create client context object
    ctx = ClientContext(sharepoint_url).with_credentials(user_credentials)
    print(ctx)
    return ctx


def create_sharepoint_directory(dir_name: str):
    """
    Creates a folder in the sharepoint directory.
    """
    if dir_name:
        ctx = get_sharepoint_context_using_user()
        result = ctx.web.folders.add(f'{dir_name}').execute_query()

create_sharepoint_directory('Testing')

排查与解决思路

  1. 站点域名不一致问题
    代码中sharepoint_url配置的是https://abx.sharepoint.com,但错误返回的请求URL是https://xyz.sharepoint.com,域名完全不匹配。这会导致认证上下文指向的站点和实际请求的站点脱节,直接触发权限校验失败。先修正代码里的sharepoint_url为正确的https://xyz.sharepoint.com。

  2. 用户权限验证
    确认所用账号xyz@abc.com是否拥有目标SharePoint站点的文件夹创建权限(至少需要编辑权限)。可以手动登录该站点,尝试手动创建文件夹,验证权限是否正常。

  3. 文件夹路径完整性
    当前代码直接调用ctx.web.folders.add(dir_name),默认是在站点根目录创建文件夹。如果目标路径不是根目录,需要指定完整的服务器相对路径,比如/sites/YourSite/Shared Documents/Testing,否则可能因路径不存在或权限范围问题报错。

  4. 大文件上传额外配置
    虽然当前错误出现在文件夹创建阶段,但后续上传200MB大文件时,需要注意SharePoint的默认上传限制,建议启用分片上传:

    from office365.sharepoint.files.file import File
    # 分片上传示例
    file_path = "path/to/200mb/file"
    target_folder = ctx.web.get_folder_by_server_relative_url("Shared Documents/Testing")
    with open(file_path, 'rb') as content_file:
        file_content = content_file.read()
        File.save_binary(ctx, target_folder.serverRelativeUrl + "/large_file.bin", file_content, chunk_size=10*1024*1024)
        ctx.execute_query()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 03:24:20