如何用Python通过Graph API在SharePoint Online文档库根目录创建文件夹
正确的POST请求路径
在文档库根目录创建文件夹,需要POST到根目录的子项集合接口,正确路径为:
https://graph.microsoft.com/v1.0/drives/{drive_id}/root/children
修正后的代码
# 已获取access_token,权限为Files.ReadWrite.All和Sites.ReadWrite.All driveid = 'b!qyL8-Uu96Uud....JR4eRG9FpH-Z5' name = 'Drafts' headers = {'Authorization': 'Bearer ' + access_token} # 修正后的根目录创建文件夹路径 folder_path = 'https://graph.microsoft.com/v1.0/drives/{drive_id}/root/children' folder_path_values = folder_path.format(drive_id=driveid) new_folder = { "name": name, "folder": {}, "@microsoft.graph.conflictBehavior": "replace" } res = requests.post(folder_path_values, json=new_folder, headers=headers) # 可选:打印状态码和响应内容用于排查 print(res.status_code) print(res.json())
之前错误路径的原因分析
https://graph.microsoft.com/v1.0/drives/{drive_id}:此路径是获取Drive资源的GET接口,不支持POST创建子项,返回400符合API规范。https://graph.microsoft.com/v1.0/drives/{drive_id}/root:/:此路径指向根目录资源本身,仅支持GET/UPDATE等操作,无法直接POST创建子文件夹。https://graph.microsoft.com/v1.0/drives/{drive_id}/root:/children:语法错误,root:/是路径寻址语法,访问子项集合应使用root/children格式。
内容的提问来源于stack exchange,提问作者Cord
相关产品推荐
相关产品推荐

