使用office365-rest-python-api在SharePoint创建文件夹遇权限问题及解决
问题现象
在SharePoint中注册了一个对站点拥有读写权限的应用,想要创建包含子文件夹的层级文件夹,使用官方文档提供的代码:
def create_folder(dir_path): ctx = auth() target_folder_url = dir_path target_folder = ctx.web.ensure_folder_path(target_folder_url).execute_query() print(target_folder.serverRelativeUrl)
调用create_folder("/Shared Documents/Archive/2022/09/01")时,出现以下403权限错误:
('-2147024891, System.UnauthorizedAccessException', 'Access denied.', "403 Client Error: Forbidden for url: https://<tenant>.sharepoint.com/sites/test_site/_api/Web/RootFolder/Folders/Add('Shared%20Documents'))
将该API地址粘贴到浏览器访问时,返回错误:
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <m:code>-1, Microsoft.SharePoint.Client.ClientServiceException</m:code> <m:message xml:lang="en-US">The HTTP method 'GET' cannot be used to access the resource 'Add'. The operation type of the resource is specified as 'Default'. Please use correct HTTP method to invoke the resource.</m:message> </m:error>
问题原因及解决方法
原应用权限配置仅设置了Read和Write权限,不足以支持ensure_folder_path方法创建层级文件夹:
<AppPermissionRequests AllowAppOnlyPolicy="true"> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Read"/> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Write"/> </AppPermissionRequests>
需修改为Full Control权限:
<AppPermissionRequests AllowAppOnlyPolicy="true"> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl"/> </AppPermissionRequests>
内容的提问来源于stack exchange,提问作者martin
相关产品推荐
相关产品推荐

