python get请求下载excel_用Python下载Sharepoint Excel文件

I'm trying to download an Excel file from a SharePoint repository using a Python script. I'm using the Office365-Rest-Python-Client as defined in the examples at https://github.com/vgrem/Office365-REST-Python-Client and I have access to all the files/directories I need. The problem comes when I want to download any of the files. I've tried several approaches, but none of them works: wget.download("https://shprepos.com/path/file.xlsx", local_path, bar=None)

But I get a "403 FORBIDDEN" error. And I also tried with requests:

req = requests.get(ruta, auth=requests.auth.HTTPBasicAuth(username, password), headers=headers)

with open(local_file, 'wb') as file:

file.write(req.content)

And with this code I'm getting the webpage, not the excel file, and I don't understan why, because if I access the url "https://shprepos.com/path/file.xlsx", with the correct authentication I download the file.

Do you know a way of downloading that file with wget using the authentication? Or am I doing something wrong in the requests.get ?

I need a way of getting that file, using the previous authentication I did at the begining of the script:

ctx_auth = AuthenticationContext(shp_url)

token = ctx_auth.acquire_token_for_user(username, password)

Do you know a way of doing this? Maybe the python Client has a method for downloading the files but I can't find it!

Thank you very much! :)

Regards

解决方案

Yep! I found the solution!! I needed to get the authorization before I can download the file. I found an example within the test folder of the Office365-Python-Client. So basically, before getting the url with the request, you get the authorization:

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)

If you don't get the auth_request and add the headers, you can't get the file.

Hope it helps somebody in the future as worked for me!

Any improvement is more than welcome!! :)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值