can anyone please help me with how to download a file from SharePoint? I've been going through this code all day and haven't figured anything out. I can access a specific file and its URL. However, when I try to download it using the code below, it gives me a 401 error even though the token is correct (I've tried both access and refresh tokens). I tryed it also load into dataframe but without success.
token handling token_backend = FileSystemTokenBackend(token_path='my_folder', token_filename='my_token.txt')
scopes scopes = ['https://graph.microsoft.com/.default', 'offline_access']
setup connection credentials = (CLIENT_ID, SECRET_ID) account = Account(credentials=credentials, auth_flow_type='authorization', scopes=scopes, token_backend=token_backend, tenant_id=TENANT_ID) conn = Connection(credentials)
connect to cloud if not account.is_authenticated: # will check if there is a token and has not expired account.authenticate() else: token_backend.check_token() account.con.refresh_token()
Get data from SharePoint site
sp_site = account.sharepoint().get_site('root', '/sites/mysite)
Get SharePoint lists
lists = sp_site.get_lists()
# Print SharePoint lists
for sharepoint_list in lists:
print(f"Name: {sharepoint_list.name}")
And when I find the file, I wanted to download it like this:
access_token = token_backend.get_token()
# Headers with access token
headers = {
'Authorization': f'Bearer {access_token}'
}
# Download file
response = requests.get(file_url, headers=headers, verify=False)
And after that I get 401 ... Can anyone help me? Thank you guys