Uncategorized

Download files from SharePoint via Python script


I am attempting to utilize the following Python script to access documents stored in SharePoint and save them to a local folder. However, I am encountering the following error:

ValueError: {“error”:”invalid_request”,”error_description”:”AADSTS900023: Specified tenant identifier ‘none’ is neither a valid DNS name, nor a valid external domain. Trace ID: 1cf65f21-719a-4de7-89e1-9b8687d3e506 Correlation ID: ac673258-53eb-420c-9ebc-1fb9ecaf7602 Timestamp: 2024-01-10 18:05:06Z”,”error_codes”:[900023],”timestamp”:”2024-01-10 18:05:06Z”,”trace_id”:”1cf65f21-719a-4de7-89e1-9b8687d3e506″,”correlation_id”:”ac673258-53eb-420c-9ebc-1fb9ecaf7602″,”error_uri”:”https://accounts.accesscontrol.windows.net/error?code=900023″}

This is my code:

import os
import re
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File

site_url = "https://contoso.sharepoint.com.mcas.ms/sites/Contoso/"
client_id = "id"
client_secret = "secret"
credentials = ClientCredential(client_id, client_secret)
ctx = ClientContext(site_url).with_credentials(credentials)

def get_files_list(folder_url, ctx):
    root_folder = ctx.web.get_folder_by_server_relative_url(folder_url)
    root_folder.expand(\["Files", "folders"\]).get().execute_query()
    return root_folder.files

def download_file(file_name, folder_url, ctx):
    file_url = f'{folder_url}/{file_name}'
    file = File.open_binary(ctx, file_url)
    return file.content

def save_file(file_name, file_object):
    with open(f'/data/teste/{file_name}', 'wb') as f:
        f.write(file_object)
        print(f"{file_name} downloaded.")

folder_url = "/sites/Contoso/Documentos%20Compartilhados/ContosoDocs?"

list_of_files = get_files_list(folder_url, ctx)
for file in list_of_files:
    file_object = download_file(file.name, folder_url, ctx)
    save_file(file.name, file_object)

Could someone please provide guidance on resolving this issue?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *