0

I'm using the python office365 library and am repeatedly coming up against the same problem, encapsulated below where I'm trying to add a file:

After setting the site url, folder etc., this is the code:

my_folder = f"{siteurl}/{my_path}"

from office365.runtime.auth.authentication_context import UserCredential from office365.sharepoint.folders.folder import Folder

credentials = UserCredential(username, password)

content = b"Some content" folder = Folder.from_url(my_folder).with_credentials(credentials) files = folder.get_files().execute_query() for file in files: print(file.name)

newfile = files.add("myfile.txt", content, overwrite=True).with_credentials(credentials) newfile.execute_query()

The response I get is:

weights.xlsx
rut955_notes
BMI Chart.xlsx
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/office365/runtime/client_request.py", line 58, in execute_query
    response.raise_for_status()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: FORBIDDEN for url: https://company.sharepoint.com/sites/mysite/folder1/_api/Web/getFolderByServerRelativeUrl('%2Fsites%2Fmysite%2Ffolder1%2Ffolder2%2Ffred')/Files/add(overwrite=true,url='myfile.txt')

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/rog/tmp/sp/./p3", line 24, in <module> newfile.execute_query() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/office365/runtime/client_object.py", line 44, in execute_query self.context.execute_query() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/office365/runtime/client_runtime_context.py", line 181, in execute_query self.pending_request().execute_query(qry) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/office365/runtime/client_request.py", line 62, in execute_query raise ClientRequestException(*e.args, response=e.response) office365.runtime.client_request_exception.ClientRequestException: ('-2130575251, System.Runtime.InteropServices.COMException', 'The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.', "403 Client Error: FORBIDDEN for url: https://company.sharepoint.com/sites/mysite/folder1/_api/Web/getFolderByServerRelativeUrl('%2Fsites%2Fmysite%2Ffolder1%2Ffolder2%2Ffred')/Files/add(overwrite=true,url='myfile.txt')")

So I can see the three files already in the folder (weights.xlsx, rut955_notes and BMI Chart.xlsx) but I can't create the new one.

If I use a web interface and go to siteurl/my_path, I can see the same files and upload a new one without problem while logged in as the same user.

Using a different library that sets a file content using a PUT directly to the address of the new file (siteurl/mypath/filename.txt) works fine with the same credentials.

The failing request from the office365 library is using a POST to https://company.sharepoint.com/sites/mysite/folder1/_api/Web/getFolderByServerRelativeUrl('%2Fsites%2Fmysite%2Ffolder1%2Ffolder2%2Ffred')/Files/add(overwrite=true,url='myfile.txt') so they're quite different methods.

Any help welcome!

0 Answers0