Copy and paste file from sharepoint using pyhton and Office365-REST-Python-Client

58 Views Asked by At

I am using Office365-REST-Python-Client and I want to copy and paste a file on sharepoint. I am following the example from here but I am still getting error.

the code:

from office365.sharepoint.client_context import ClientContext
ctx = ClientContext(sharepoint_url, context_auth)

file_from = ctx.web.get_file_by_server_relative_path("Shared Documents/Subfolder/my_file.xlsx")
folder_to = ctx.web.get_folder_by_server_relative_path("Shared Documents/Subfolder")
file_to = file_from.copyto_using_path(folder_to, True).execute_query()
print("{0} copied into {1}".format(file_from, file_to))

Error: ClientRequestException: (None, None, "401 Client Error: Unauthorized for url: https://mysite.sharepoint.com/\_api/Web/getFileByServerRelativePath(DecodedUrl='Shared%20Documents%2FSubfolder%2Fmy_file.xlsx')/CopyToUsingPath(DecodedUrl=%3Coffice365.sharepoint.folders.folder.Folder%20object%20at%200x7f45e7f0f310%3E,bOverWrite=true)")

I have access and permissions to the file/folder cause I created the file in the step before.

0

There are 0 best solutions below