I want to download a file from my sharepoint 2016 server using Python.

I am using the requests module, but if another module is better, can use that one instead.

from requests.auth import HTTPBasicAuth
import requests

headers = {'accept': 'application/json;odata=verbose'}
auth = HTTPBasicAuth("username", "password")
r = requests.get("https://example.sharepoint.com/_api/web", auth=auth, headers=headers)

But my request gets redirected to my https://example.sharepoint.com/my.policy page with HTML data that contains the following:

Session Expired/Timeout Due to user inactivity, your session expired. Click the following link to start a new session.

I also tried:

from requests_ntlm import HttpNtlmAuth
auth = HttpNtlmAuth("username", "password")
r = requests.get("https://example.sharepoint.com/_api/web", auth=auth, headers=headers)

but got the same redirect to .

This seems similar to the authentication prompt issue described in this SE: SharePoint 2016 - ADFS - persistent cookie - office client integration - authentication prompt

I think I need to include an authentication cookie with my request somehow, but I could be wrong. Any idea what the authentication/persistant cookie object looks like?

1 Answer 1


You will need to request an auth token from the service. There is a good sample project here.


  • 1
    Using Office365-REST-Python-Client worked for me. This module seems to handle the auth token needed for Sharepoint 2016 whereas my solutions didn't.
    – crusarovid
    Jan 11, 2018 at 23:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.