尝试将位于 SharePoint 中的 Excel 文件下载到我的 Cloudera Workbench 目录中时, 我收到以下错误
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.files.file import File
app_principal = {
'site_url' : 'https://XXXXXXX.sharepoint.com/sites/XXXXXX',
'client_id' : 'XXXXXXXX',
'client_secret' : 'XXXXXXXX',
'folder_url_shrpt' : """/sites/XXXXXXXX/Shared%20Documents/General/Testing_data"""
}
ctx_auth = AuthenticationContext(app_principal['site_url'])
ctx_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])
ctx = ClientContext(app_principal['site_url'], ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print('Authenticated into sharepoint as: ',web.properties['Title'])
file_url = """/sites/XXXXXXXX/Shared%20Documents/General/Testing_data/train_data.xlsx"""
download_path = pathlib.Path().resolve() / "download_file.xlsx"
with open(download_path, "wb") as local_file:
file = ctx.web.get_file_by_server_relative_path(file_url).download(local_file).execute_query()
#file = ctx.web.get_file_by_server_relative_url(file_url).download(local_file).execute_query()
print("[Ok] file has been downloaded into: {0}".format(download_path))
--------------------------
---------------------------------------------------------------------------
ClientRequestException: (Microsoft.SharePoint.SPException', 'The file
/sites/XXXXXXXXX/Shared%20Documents/General/Testing_data/train_data.xlsx does not exist.', "404 Client Error: Not Found for url: https://XXXXXXXX.sharepoint.com/sites/XXXXXXXX/_api/Web/getFileByServerRelativePath......
我从Github获取了代码https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/files/download_file.py
请帮忙!
该问题可能与自编码 URL(“Shared%Documents”)甚至权限问题有关。下面的代码首先列出提供的文件夹中的所有文件,然后下载特定文件。当下载名称中包含特殊字符的文件时,这也适用。
from office365.sharepoint.client_context import ClientContext
app_principal = {
'site_url' : 'https://XXXXXXX.sharepoint.com/sites/XXXXXX',
'client_id' : 'XXXXXXXX',
'client_secret' : 'XXXXXXXX',
'folder_url_shrpt' : '/sites/XXXXXXXX/Shared Documents/General/Testing_data'
}
# Authenticate to the sharepoint site using client-id and client-secret
ctx = ClientContext(app_principal['site_url']).with_client_credentials(client_secret=app_principal['client_secret'], client_id=app_principal['client_id'])
folder_relative_url = app_principal['folder_url_shrpt']
filename_to_be_downloaded = 'train_data.xlsx'
download_path = pathlib.Path().resolve() / 'download_file.xlsx'
# List all files within the folder
folder = ctx.web.get_folder_by_server_relative_path(folder_relative_url)
ctx.load(folder)
ctx.execute_query()
files = folder.files
ctx.load(files)
ctx.execute_query()
# Filter the file by name which needs to be downloaded
for file in files:
if file.properties["Name"] == filename_to_be_downloaded:
file_content = file.get_content().execute_query()
with open(download_path, 'wb') as f:
f.write(file_content.value)
print("[Ok] file has been downloaded into: {0}".format(download_path))
break
如果它解决了您的问题,请告诉我。