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

使用Microsoft Graph API的CreateUploadSession上传至SharePoint遇400错误求助

调用Microsoft Graph API查询ShareDrive目录时出现400 Bad Request错误

我尝试用Python通过Microsoft Graph API的CreateUploadSession接口向SharePoint写入文件,已成功获取令牌且能正常读取SharePoint内容,但调用requests.get(f'{ENDPOINT}/drives/{drive_id}/root:/{folder_url}', headers=headers)时触发400 Bad Request错误。

相关代码及错误信息

import requests
import msal
import atexit
import os.path
import urllib.parse
import os

TENANT_ID = '<your tenant id>'
CLIENT_ID = '<your application id>'
SHAREPOINT_HOST_NAME = 'yourcompany.sharepoint.com'
SITE_NAME = '<your site name>'

AUTHORITY = 'https://login.microsoftonline.com/' + TENANT_ID
ENDPOINT = 'https://graph.microsoft.com/v1.0'    
filename = 'test_file.txt'
folder_path = "C:/Users/svcAlteryx-DEV/Desktop"

folder_url = urllib.parse.quote(folder_path)
result = requests.get(f'{ENDPOINT}/drives/{drive_id}/root:/{folder_url}', headers=headers)
print(f'{ENDPOINT}/drives/{drive_id}/root:/{folder_url}')
result.raise_for_status()

错误信息:

HTTPError: 400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/drives/b!RISMyiDdukGGGNeDPsCAu_mfncDFdCNCjwSApVwoJ7R2lYZfKplTR4Lv3IkATh-p/root:/C%3A/Users/svcAlteryx-DEV/Desktop

已成功完成的操作

我已通过以下代码成功获取drive_id(返回状态码200):

result = requests.get(f'{ENDPOINT}/sites/{site_id}/drive', headers=headers)
result.raise_for_status()
drive_info = result.json()
drive_id = drive_info['id']

后续计划执行的代码

folder_info = result.json()
folder_id = folder_info['id']

file_url = urllib.parse.quote(filename)
result = requests.post(
    f'{ENDPOINT}/drives/{drive_id}/items/{folder_id}:/{file_url}:/createUploadSession',
    headers=headers,
    json={
        '@microsoft.graph.conflictBehavior': 'replace',
        'description': 'A large test file',
        'fileSystemInfo': {'@odata.type': 'microsoft.graph.fileSystemInfo'},
        'name': filename
    }
)
result.raise_for_status()
upload_session = result.json()
upload_url = upload_session['uploadUrl']

问题原因及修正方案

核心错误是你把本地系统的文件路径(C:/Users/svcAlteryx-DEV/Desktop)直接当成了SharePoint文档库内的路径传给了Graph API。Graph API中root:/{path}语法里的path指的是SharePoint站点文档库内的相对路径,和本地路径无关。

修正步骤:

  1. 替换目标路径:把folder_path改成SharePoint文档库中你要操作的目录路径。比如要传到文档库根目录下的测试上传文件夹,就写folder_path = "测试上传文件夹";如果是根目录,直接用folder_path = ""或者省略路径部分。
  2. 移除不必要的URL编码:Graph API支持直接传入未编码的路径(含空格等特殊字符也可),不需要额外用urllib.parse.quote处理,除非路径包含URL保留字符。

修正后的查询代码示例:

# 示例:查询SharePoint文档库根目录下的"测试上传文件夹"
folder_path = "测试上传文件夹"
result = requests.get(f'{ENDPOINT}/drives/{drive_id}/root:/{folder_path}', headers=headers)
result.raise_for_status()

后续上传会话的简化建议:

如果目标文件夹是文档库根目录,无需先查询folder_id,可以直接调用:

result = requests.post(
    f'{ENDPOINT}/drives/{drive_id}/root:/{filename}:/createUploadSession',
    headers=headers,
    json={
        '@microsoft.graph.conflictBehavior': 'replace',
        'name': filename
    }
)
result.raise_for_status()
upload_session = result.json()
upload_url = upload_session['uploadUrl']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:00:30