首页 > 解决方案 > 尝试通过 Python 访问共享点以下载文件

问题描述

import os
import tempfile
from office365.runtime.auth.client_credential import ClientCredential
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
user_credentials = UserCredential('username', 'password')
ctx = ClientContext('https://work.sharepoint.com').with_credentials(user_credentials)

file_url = '/sites/work/Shared Documents/Beta Testing/Practice.xlsx'
download_path = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
with open(download_path, "wb") as local_file:
    file = ctx.web.get_file_by_server_relative_path(file_url).download(local_file).execute_query()
print("[Ok] file has been downloaded: {0}".format(download_path))

尝试使用此代码访问然后从共享点下载特定文件(出于明显的原因,所有机密部分都被编辑掉),每当我尝试此操作时,我要么得到 404 Client Error Not Found for url: 要么 400 Client Error :对 url 的错误请求:如果我在 file_url 中的站点/工作之前添加 url 的 ctx 部分。

标签: excelsharepointoffice365

解决方案


推荐阅读