用Python下载Sharepoint Excel文件

我试图使用Python脚本从SharePoint存储库下载Excel文件。 我使用的是https://github.com/vgrem/Office365-REST-Python-Client中示例中定义的Office365-Rest-Python-Client,我可以访问我需要的所有文件/目录。 问题出现在我想下载任何文件的时候。 我已经尝试了几种方法,但都没有工作: wget.download("https://shprepos.com/path/file.xlsx", local_path, bar=None)

但是我得到一个“403 FORBIDDEN”错误。 我也尝试过请求:

 req = requests.get(ruta, auth=requests.auth.HTTPBasicAuth(username, password), headers=headers) with open(local_file, 'wb') as file: file.write(req.content) 

与此代码,我得到的网页,而不是Excel文件,我不明白为什么,因为如果我访问URL“ https://shprepos.com/path/file.xlsx ”,与正确的身份validation我下载文件。

你知道用wget使用身份validation下载该文件的方法吗? 或者我在requests.get中做错了什么?

我需要一种获取该文件的方法,使用之前在脚本开始时进行的身份validation:

 ctx_auth = AuthenticationContext(shp_url) token = ctx_auth.acquire_token_for_user(username, password) 

你知道这样做吗? 也许python客户端有一个下载文件的方法,但我找不到它!

非常感谢你! 🙂

问候

是的! 我find了解决办法! 我需要获得授权,然后才能下载文件。 我在Office365-Python-Client的testing文件夹中find了一个例子。 所以基本上,在获取请求的url之前,你会得到授权:

 options = RequestOptions(shp_file_path) ctx_auth.authenticate_request(options) options.headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f" options.headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0)" req = requests.get(shp_file_path, headers=options.headers, verify=True, allow_redirects=True) if req.ok: with open(local_file, 'wb') as file: file.write(req.content) 

如果您没有获得auth_request并添加标题,则无法获取该文件。

希望它有助于未来的人为我工作! 任何改进都比欢迎! 🙂