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

使用Office365-REST-Python-Client访问SharePoint遇403错误求助

SharePoint API调用403权限问题排查

问题背景

组织AD管理员已授予我SharePoint完全访问权限,但运行以下Python代码时触发403禁止错误:

from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext

site_url = "https://myname.sharepoint.com"
client_id = "xxx-xxx-xxx-xxx-xxx"
client_secret = "xxxxxxxx"

ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret))

folder_path = 'sites/SOF'
root_folder = ctx.web.get_folder_by_server_relative_path(folder_path).expand(["Files"]).get().execute_query()
print("root_folder = ", root_folder)

for file in root_folder.files:
    print(file.name)

首次执行错误

File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://myname.sharepoint.com/_api/Web/getFolderByServerRelativePath(DecodedUrl='sites%2FSOF')?$expand=Files

调整site_url后的错误

将site_url修改为https://myname.sharepoint.com/sites/SOF后,依旧报错:

requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://myname.sharepoint.com/sites/SOF/_api/Web/getFolderByServerRelativePath(DecodedUrl='')?$expand=Files

验证信息

在浏览器中直接访问对应API接口可正常获取包含数据的XML,且已确认client_id和client_secret无误。

疑问

请问还需向管理员申请哪些权限?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 07:25:09