Sharepoint Rest API office365 库 403 客户端错误

问题描述 投票:0回答:1

我正在尝试访问 Sharepoint 文件夹并检索其中的文件。

使用以下代码:

# Import libraries
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential

# Config for Sharepoint (to be stored in env later)
SP_CLIENT_ID = xxx
SP_CLIENT_SECRET = xxx
SP_URL = 'https://<organization_url>.sharepoint.com/sites/<site-name>'
relative_folder_url = '/Shared%20Documents/<subfolder>'

# App-based authentication with access credentials
context = ClientContext(SP_URL).with_credentials(ClientCredential(SP_CLIENT_ID, SP_CLIENT_SECRET))
folder = context.web.get_folder_by_server_relative_url(relative_folder_url)
context.load(folder)
context.execute_query()

# Processes each Sharepoint file in folder
for file in folder.files:
    print(f'Processing file: {file.properties["Name"]}')

但是,它会抛出错误

office365.runtime.client_request_exception.ClientRequestException: ('-2147024891, System.UnauthorizedAccessException', 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))', "403 Client Error: Forbidden for url: https://<tenant-name>.sharepoint.com/sites/<site-id>/_api/Web/getFileByServerRelativePath(DecodedUrl='%2Fsites%2F<site-name>%2FDocuments%2FGeneral%2F<subfolder>')")` stemming from `context.execute_query()

with the specific names replaced by <xxx>.

我可以使用

Graph API
上的
Postman
检索文件,使用相同的
SP_CLIENT_ID
SP_CLIENT_SECRET
。这是通过使用
client ID
secret
生成令牌,然后在 Graph GET API 调用中传递令牌来完成的。

基于应用程序的身份验证似乎没问题,因为错误是

403

如何修复该问题,以便可以通过 Python 上的 Sharepoint Rest API 以编程方式查看(并最终下载)文件?

编辑: 该错误可能是由于我用来对 Sharepoint 进行身份验证的 Azure 应用程序上缺少 Sharepoint Rest API 授予的权限。 现在我在想我是否应该继续使用

office365
或者简单地使用一些纯 API 方法,例如 Python 中的
requests
库以编程方式下载 Sharepoint 文件。

python sharepoint azure-active-directory azure-ad-graph-api sharepoint-rest-api
1个回答
0
投票

我有一个名为 sritestsite10 的 SharePoint 网站,以下路径中的文件很少:

enter image description here

当我在我的环境中运行你的代码时,我也遇到了相同的错误,如下所示:

enter image description here

为了解决错误,我使用 全局管理员帐户运行了下面的 URL,并添加了具有租户权限的仅应用程序主体,如下所示:

https://xxx-admin.sharepoint.com/_layouts/15/appinv.aspx
您可以通过输入 

AppId

 找到您的 Azure AD 应用程序,并使用以下 xml 添加权限:

<AppPermissionRequests AllowAppOnlyPolicy="true"> <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" /> </AppPermissionRequests>

enter image description here

确保单击

信任它选项以授予权限:

enter image description here

当我通过将

.expand(["Files"])

 添加到文件夹变量再次运行代码时,我成功获得了 response,如下所示:

# Import libraries from office365.sharepoint.client_context import ClientContext from office365.runtime.auth.client_credential import ClientCredential # Config for Sharepoint (to be stored in env later) SP_CLIENT_ID = xxx SP_CLIENT_SECRET = xxx SP_URL = 'https://<organization_url>.sharepoint.com/sites/<site-name>' relative_folder_url = '/Shared%20Documents/<subfolder>' # App-based authentication with access credentials context = ClientContext(SP_URL).with_credentials(ClientCredential(SP_CLIENT_ID, SP_CLIENT_SECRET)) folder = context.web.get_folder_by_server_relative_url(relative_folder_url).expand(["Files"]) context.load(folder) context.execute_query() # Processes each Sharepoint file in folder for file in folder.files: print(f'Processing file: {file.properties["Name"]}')

回复:

enter image description here

参考: azure - HTTPError:403 客户端错误:禁止通过 Office365-REST-Python-Client 获取 url - 堆栈溢出,作者:Rukmini

© www.soinside.com 2019 - 2024. All rights reserved.