'How to download a file from Sharepoint with python

I need to download a file from sharepoint using user credentials (rather that client credentials).

I've tried this:

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File

root_url = "https://company-my.sharepoint.com"
full_url = "https://company-my.sharepoint.com/personal/Documents/AB.csv"

ctx = ClientContext(root_url)
ctx.with_user_credentials(<my_email>,
                          <my_password)

response = File.open_binary(ctx, full_url)
print(response.content)

The response I am getting is

*b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"serverRelativePath\\r\\nParameter
name: Specified value is not supported for the serverRelativePath
parameter."}}}'*


Solution 1:[1]

Use ServerRelativeUrl, eq. /sites/site/libraryname/folder/filename.ext. Function returned File object. In your case, ServerRelativeUrl should be

response = File.open_binary(ctx, "/personal/Documents/AB.csv")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RaytheonXie-MSFT