Skip to main content

SharePy - 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:

s.save()

Later, the load function can be used to restore the session:

s = sharepy.load()

The default file name for saving and loading sessions is sp-session.pkl, however an alternative location can be provided as an argument to save() and load().

Advanced usage

Requests authentication

SharePy implements Requests authentication classes that can also be used directly with Requests itself:

import requests
import sharepy

auth = 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 sites
  • SharepointADFS - For ADFS-enabled sites

Custom authentication URL

The authentication URL is detected automatically when using sharepy.connect(). If a different URL is required for a region-specific account, it can be specified by manually creating an auth object and setting its login_url property:

import sharepy

auth = sharepy.auth.SharePointOnline(username="user@example.com")
auth.login_url = "https://login.microsoftonline.de/extSES.srf"
s = sharepy.SharePointSession("example.sharepoint.com", auth)

Useful reading

Licence

This software is distributed under the GNU General Public License v3. Copyright 2016-2021 Jonathan Holvey.

Credits

  1. The authentication method used here is based on this post by Luc Stakenborg.
  2. Additional help regarding request digests from sadegh's comment on this post by Paul Ryan.
  3. Contributed code from @joemeneses for ADFS authentication.

Release files for sharepy 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sharepy 2.0.0
File Size Uploaded
sharepy-2.0.0.tar.gz 23.2 kB Details

Release files / sharepy-2.0.0.tar.gz

Download URL sharepy-2.0.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
683ba3d472b9dfa259e01f3b9e8a4aea3abc833fb01ca2ca1c22e618301ded1a
BLAKE2b-256 checksum
How to use checksums
39def27c086bce54884a8538a22878ebf9e3a803a66c27b5d604d02538d93b30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.6.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.10

Release history Release notifications | RSS feed

This release

2.0.0 This release

1 release file

1.3.0

1 release file

1.2.0

1 release file

1.1.3

1 release file

1.1.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page