3

I'm trying to use python to download an excel file that is hosted in a sharepoint which is part of the Microsoft Azure platform. I tried to retrieve the file with HTTPforhumans's request by doing:

r = requests.get(url)

But my requests keep getting denied (r.status_code returns 200) because I need to login to a valid account before trying to access the file. I do have a valid account and password, and I can access to my account and to the excel file via the browser. But I have no idea how to deal wit the Azure authentication procedure. And apparently it is not as easy as just doing:

auth = HTTPBasicAuth('[email protected]', 'pass1234')
r = requests.post(url=url, auth=auth)

It is my uderstanding that there's a flow to follow, but when I try to read the documentation, it just goes over my head (I'm an engineer and I do not have experience in this kind of environment).

Can someone guide me in the process of how to login and download the file?

1 Answer 1

3

Try O365 rest python client library.it supports SharePoint Online authentication and allows to download/upload a file as demonstrated below: Please find the code here:

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username,password)   
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(context, "/Shared Documents/User Guide.docx")
with open("./User Guide.docx", "wb") as local_file:
    local_file.write(response.content)

You can download the latest version using below command

pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git

For further reference please visit link

hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the answer, it seems to work. Nonetheless now I'm getting: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access '' - Which I guess has more to do with the configuration that my organization has. I'll be posting a new question with this issue.
Glad to know it helped. Please feel free to open new thread and tag me , will be happy to assist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.