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

使用Microsoft Graph API创建UploadSession时遇'root:'资源未找到错误

调用Microsoft Graph API创建上传会话返回BadRequest错误

尝试从Databricks向SharePoint上传大文件,调用createUploadSession接口创建上传会话时,令牌已正常验证,但始终返回BadRequest错误,提示“Resource not found for the segment 'root:'”。调整请求URL、请求体、文件夹路径及文件名后问题仍未解决。

相关代码

output_folder = "Databricks_Sharepoint_upload_test"
file_name = "test_file.csv"
input_path = "/dbfs/FileStore/tables/sharepoint_input/test_file.csv"

request_body = {
'item': {
"@odata.type": "microsoft.graph.driveItemUploadableProperties",
"@microsoft.graph.conflictBehavior": "replace",
'name': file_name,
"fileSize": os.path.getsize(input_path)
}
}

headers = {
'Authorization': 'Bearer ' + token
}

response_upload_session = requests.post(f'https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{drive_id}/root:/{output_folder}:/{file_name}:/createUploadSession', headers = headers, json = request_body)

print(response_upload_session.json())

返回错误信息

{'error': {'code': 'BadRequest', 'message': "Resource not found for the segment 'root:'.", 'innerError': {'date': '2024-04-05T12:48:09', 'request-id': '', 'client-request-id': ''}}}

解决方法

  • 修正请求URL格式:原URL中路径拼接错误,把/root:/{output_folder}:/{file_name}:/createUploadSession改成/root:/{output_folder}/{file_name}:/createUploadSession——多余的冒号导致Graph API无法正确解析文件路径,引发“root:”段找不到的错误。
  • 确认目标文件夹存在:如果Databricks_Sharepoint_upload_test文件夹不存在,建议先调用Graph API创建该文件夹,大文件上传会话通常要求父文件夹已存在,否则可能触发路径解析错误。
  • 简化请求体(可选):fileSize参数并非必须,移除后可避免本地计算的文件大小与实际上传时不一致的潜在问题,让Graph API自动处理文件大小。

修正后的代码示例

import os
import requests

output_folder = "Databricks_Sharepoint_upload_test"
file_name = "test_file.csv"
input_path = "/dbfs/FileStore/tables/sharepoint_input/test_file.csv"

request_body = {
    'item': {
        "@odata.type": "microsoft.graph.driveItemUploadableProperties",
        "@microsoft.graph.conflictBehavior": "replace",
        'name': file_name
    }
}

headers = {
    'Authorization': 'Bearer ' + token
}

# 修正路径拼接格式
response_upload_session = requests.post(
    f'https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{drive_id}/root:/{output_folder}/{file_name}:/createUploadSession',
    headers=headers,
    json=request_body
)

print(response_upload_session.json())

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 13:02:49