Simple SharePoint Online authentication for Python
Likely abandoned. No release or commit for years (last release over 4 years ago).
Consider insteadoffice365-rest-python-clientPythie’s Index: 8210.4M downloads/moCompare →
pip install sharepySharePy - Simple SharePoint Online authentication for Python
This module will handle authentication for your SharePoint Online/O365 site, allowing you to make straightforward HTTP requests from Python. It extends the commonly used Requests module, meaning that returned objects are familliar, easy to work with and well documented.
Installation
SharePy can be installed from the Python Package Index, PyPI.
pip install sharepy
Initiating a SharePoint session
import sharepy
s = sharepy.connect("example.sharepoint.com")
You will be prompted to enter your username and password, which are used to request a security token from Microsoft. An access cookie and request digest token are then retrieved and saved to properties for later use. The digest token will be refreshed automatically as it expires.
A username and password can also be provided as arguments of the connect function, if prompts are not desirable.
Making API calls
r = s.get("https://example.sharepoint.com/_api/web/lists/GetByTitle('Test Library')")
This will return a Requests response object. See the requests documentation for details. By default, the headers Accept: application/json; odata=verbose and Content-type: application/json; odata=verbose are sent with all requests, so API responses will be formatted as JSON where available.
Headers can be added or overridden by supplying a dictionary to the relevant method:
r = s.get("https://example.sharepoint.com/_api/...", headers={"Accept": "application/atom+xml"})
The request will send a digest header, allowing modifications to be made to SharePoint objects.
Downloading a file
r = s.getfile("https://example.sharepoint.com/Library/Test%20File.pdf")
This will download the file to the current directory and return a response object. Alternatively you can specify a location to save the file to:
r = s.getfile("https://example.sharepoint.com/Library/Test%20File.pdf", filename="downloads/file.pdf")
Saving an authenticated session
Properties of the authentication session can be saved to a file using the save method, so the session can be used without having to re-authenticate each time a program is run: