JonathanHolvey / sharepy

Simple SharePoint authentication for Python
GNU General Public License v3.0
175 stars 52 forks source link

Send DELETE API Calls via Sharepy #47

Closed Cable-1406 closed 3 years ago

Cable-1406 commented 3 years ago

Hey I wanted to ask if there is a way to send delete API Calls via the sharepy Library as there is no requests equivalent of delete() in your source code.

Grateful for any helping hand.

JonathanHolvey commented 3 years ago

In the current release, the auth headers are sent explicitly for the post method, but nothing else.

Currently only the post() method will send a digest header, allowing modifications to be made to SharePoint objects

This means that, although you should be able to call the delete method, you'll probably get an authentication error.

There is a beta release branch for SharePy v2, which uses a Requests authentication class, which should handle all HTTP methods transparently. I'd recommend you give this a try.

https://github.com/JonathanHolvey/sharepy/tree/release-2.0.0-beta

Cable-1406 commented 3 years ago

Thanks for the quick reply! So with the beta branch I should be able to use delete() exactly like get() and post() in the previous version?

JonathanHolvey commented 3 years ago

Yes, you should be able to.

When I originally wrote SharePy, I only ever used the POST method, so I didn't implement authentication for the others. The v2 beta delegates authentication to Requests, so every HTTP method will include the necessary auth headers.

I haven't been able to release v2, due to lack of testing. Let me know how you get on and I might be able to finally release it.

Cable-1406 commented 3 years ago

Hm, so I downloaded the zip archive of the 2.0.0 beta and imported it locally, but it I still get the SPException whenever I try to delete something.

I tried something like this:

from Local import sharepy as sharepy

s = sharepy.connect(server_url, username=user, password=passwd)

uri = "https://example.sharepoint.com/sites/Test-Site/_api/web/folders/add('/sites/Test-Site/Freigegebene Dokumente/test')"
r = s.post(uri)

uri = "https://example.sharepoint.com/sites/Test-Site/_api/web/GetFolderByServerRelativeUrl('/sites/Test-Site/Freigegebene Dokumente/test')"
r = s.get(uri)
r = s.delete(uri)

(I also write the answers into files, but for simplicity I did not include this in here)

Curiously the post() function does not work either. I tried switching back to the master branch version (1.3.0 I think) and the POST Call worked fine again. Maybe my import is faulty (unpacked the zip of the beta branch and imported that locally)? Le ol' Debugger showed that the beta version directly switches to the requests sessions (which is expected from what I understood of your explanation).

JonathanHolvey commented 3 years ago

Can you post the stack trace of the error you're seeing?

Cable-1406 commented 3 years ago

Well the program itself does not really throw any Errors, but the API Call Response contains an SPException:

{ "error": { "code": "-2130575251, Microsoft.SharePoint.SPException", "message": { "lang": "de-DE", "value": "Die Sicherheits\u00fcberpr\u00fcfung f\u00fcr die Seite ist ung\u00fcltig und m\u00f6glicherweise fehlerhaft. Klicken Sie im Webbrowser auf die Schaltfl\u00e4che 'Zur\u00fcck', um den Vorgang zu wiederholen." } } }

The "value" field says something along the lines of "The security clearance for the page is invalid and may contain errors." which I always get with DELETE API Calls and also POST Calls if I use the beta version.

JonathanHolvey commented 3 years ago

It turns out the digest header was missing for all HTTP methods in SharePy v2, which is the cause of the error you're seeing. I've updated the release-2.0.0-beta branch, so please download it again and see if it works.

Thanks for picking this up.