如何使用Python中的Graph API在SharePoint Online文档库中创建新的根文件夹



我可以在文档库根目录下的文件夹中创建文件夹,并到处上传文件,但我不太清楚在Drive(文档库(根目录中创建新文件夹的POST路径。我找到的所有东西都假定已经是某个默认文件夹。MS文档中提供的POST示例需要一个父项ID。我已经尝试了许多POST语法的排列。

这些似乎都不起作用:

  • 'https://graph.microsoft.com/v1.0/drives/{drive_id}'
  • 'https://graph.microsoft.com/v1.0/drives/{drive_id}/root:/'
  • 'https://graph.microsoft.com/v1.0/drives/{drive_id}/root:/children'

这是我用来在文档库根目录下创建文件夹的方法:

folder_path = 'https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{parent_item_id}/children'
folder_path_values = folder_path.format(drive_id=driveid, parent_item_id=parentid)

以下是我用来提交无效请求的代码:

# Some code to get the access_token and the API permissions we have are Files.ReadWrite.All and 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}'
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)

这给了我一个400的错误——我似乎无法找出在文档库的根目录中创建新文件夹(驱动器(的正确语法。

如果您想在根目录下创建一个文件夹,那么您可以使用PATCH

PATCH /drives/id/root:/drafts
{
"folder": {},
"@microsoft.graph.conflictBehavior": "fail"
}

代码

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:/'+name
folder_path_values = folder_path.format(drive_id=driveid)
new_folder = {
"folder": {},
"@microsoft.graph.conflictBehavior": "replace"
}
res = requests.patch(folder_path_values, json=new_folder, headers=headers)

如果您想在根目录的同一级别上有另一个文件夹,那么这是不可能的,因为驱动器只能有一个最上面的文件夹。

最新更新