Package: sharepy
SharePy - Simple SharePoint Online authentication for PythonThis module will handle authentication for your SharePoint Online/O365 site, allowing you to make straightforward HTTP requests from Python. It extends the commonly usedRequestsmodule, meaning that returned objects are familliar, easy to work with and well documented.InstallationSharePy can be installed from the Python Package Index, PyPI.pip install sharepyInitiating a SharePoint sessionimportsharepys=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 theconnectfunction, if prompts are not desirable.Making API callsr=s.get("https://example.sharepoint.com/_api/web/lists/GetByTitle('Test Library')")This will return aRequestsresponseobject. See therequests documentationfor details. By default, the headersAccept: application/json; odata=verboseandContent-type: application/json; odata=verboseare 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 filer=s.getfile("https://example.sharepoint.com/Library/Test%20File.pdf")This will download the file to the current directory and return aresponseobject. 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 sessionProperties of the authentication session can be saved to a file using thesavemethod, so the session can be used without having to re-authenticate each time a program is run:s.save()Later, theloadfunction can be used to restore the session:s=sharepy.load()The default file name for saving and loading sessions issp-session.pkl, however an alternative location can be provided as an argument tosave()andload().Advanced usageRequests authenticationSharePy implements Requests authentication classes that can also be used directly with Requests itself:importrequestsimportsharepyauth=sharepy.auth.SharePointOnline(username="user@example.com")auth.login(site="example.sharepoint.com")r=requests.get("https://example.sharepoint.com",auth=auth)Available authentication classes are:SharepointOnline- For normal SharePoint Online sitesSharepointADFS- For ADFS-enabled sitesCustom authentication URLThe authentication URL is detected automatically when usingsharepy.connect(). If a different URL is required for a region-specific account, it can be specified by manually creating an auth object and setting itslogin_urlproperty:importsharepyauth=sharepy.auth.SharePointOnline(username="user@example.com")auth.login_url="https://login.microsoftonline.de/extSES.srf"s=sharepy.SharePointSession("example.sharepoint.com",auth)Useful readingConstructing SharePoint API calls:SharePoint REST API documentationHandling JSON objects in Python:Python JSON module documentationLicenceThis software is distributed under the GNU General Public License v3. Copyright 2016-2021 Jonathan Holvey.CreditsThe authentication method used here is based onthis postby Luc Stakenborg.Additional help regarding request digests from sadegh's comment onthis postby Paul Ryan.Contributed code from @joemeneses for ADFS authentication.