Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download Sharepoint Excel File in Python

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

like image 844
Sircam Avatar asked Oct 18 '22 10:10

Sircam


1 Answers

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!! :)

like image 200
Sircam Avatar answered Oct 20 '22 19:10

Sircam