snehapriya katikala
snehapriya katikala

Reputation: 25

Python script to download a file from sharepoint which is MS Authentication enabled

I need to download a file from a sharepoint which is MSA enabled.Only when i enter credentials and pass through MS authentication, i am able to view that sharepoint link and download that file.

Firstly, i want to know if we can achieve something like this via python script. To download a file from sharepoint which is MSA enabled. My code looks like below.

import urllib.request

dls="https://abc.sharepoint.com/:x:/r/sites/ProductImplementation/_layouts/15/Doc.aspx? 
   sourcedoc=%7B5FFF2D81-8FF4-47BC-ABEF-4E70413561F3%7D&file=Product%20Implementation%20Status-20240531.xlsx"
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(dls , "testngsheet.xlsx")

When i execute above code, invalid file is downloading and a part of a file content is below. 'Sign in to your account' can we achieve this using python. If not python, is there any other feasible way to automate downloading the file from sharepoint.

Upvotes: -1

Views: 32

Answers (1)

Nikolay
Nikolay

Reputation: 12245

Yes, it is possible. You can use the python SharePoint REST Client, and follow the steps in the "getting started":

Office365-REST-Python-Client

In your scenario, for authentication, it's best to use a certificate (check out the "authentication methods" chapter there).

There is also built-in synchronization in SharePoint (a button on the document library; you click that button and specify with what folder you want to synchronize it) enter image description here

Upvotes: 0

Related Questions