SharePy - Simple SharePoint Online authentication for Python
SharePy is a Python library designed for simple authentication with SharePoint Online (Office 365) sites, enabling straightforward HTTP requests. It extends the popular Requests library, providing a familiar interface for interacting with SharePoint APIs. The current stable version is 2.0.0, released on February 4, 2022. The library has a slow release cadence, with major updates being infrequent, and there are growing concerns about its compatibility with modern Microsoft 365 authentication flows.
Warnings
- breaking Version 2.0.0 introduced a major refactor, converting authentication to a standard Requests auth class. This changes the internal workings significantly from v1.x.x. Old session objects (sp-session.pkl) saved with v1.x.x are not compatible with v2.x.x and will be invalidated by major version number changes.
- breaking The `auth_tld` argument was removed from `sharepy.connect()` in v2.0.0. It is replaced by a `login_url` property in the authentication classes (e.g., `sharepy.auth.SharePointOnline`).
- gotcha SharePy might not be compatible with newer Microsoft 365 authentication models, especially when Multi-Factor Authentication (MFA) is enabled, or with federated logins (e.g., GoDaddy). Users have reported 'RTFA and authentication errors' and freezing during authentication. The traditional username/password method often fails if 'Security Defaults' are active in Azure AD.
- gotcha SSL certificate verification errors are common. This can be due to outdated `certifi` package or issues with the SharePoint server's SSL certificate.
- gotcha Incorrect SharePoint site URL format can lead to authentication errors. Specifically, a trailing slash (e.g., `https://example.sharepoint.com/` vs `https://example.sharepoint.com`) might cause issues.
Install
-
pip install sharepy
Imports
- connect
import sharepy s = sharepy.connect(...)
- SharePointOnline
from sharepy.auth import SharePointOnline
Quickstart
import os
import sharepy
SHAREPOINT_SITE = os.environ.get('SHAREPOINT_SITE', 'example.sharepoint.com')
SHAREPOINT_USERNAME = os.environ.get('SHAREPOINT_USERNAME', 'user@example.com')
SHAREPOINT_PASSWORD = os.environ.get('SHAREPOINT_PASSWORD', 'your_password')
if not all([SHAREPOINT_SITE, SHAREPOINT_USERNAME, SHAREPOINT_PASSWORD]):
print("Please set SHAREPOINT_SITE, SHAREPOINT_USERNAME, and SHAREPOINT_PASSWORD environment variables.")
exit(1)
try:
# Establish a connection to SharePoint Online
# Note: For MFA-enabled accounts, username/password might not work reliably.
# See warnings for more details.
s = sharepy.connect(SHAREPOINT_SITE, username=SHAREPOINT_USERNAME, password=SHAREPOINT_PASSWORD)
print(f"Successfully connected to {SHAREPOINT_SITE}")
# Example: Make an API call to get lists from the site
response = s.get(f'https://{SHAREPOINT_SITE}/_api/web/lists')
response.raise_for_status() # Raise an exception for bad status codes
print("SharePoint Lists:")
for item in response.json()['d']['results']:
print(f"- {item['Title']}")
# Example: Download a file (replace with a real URL)
# file_url = f'https://{SHAREPOINT_SITE}/Shared Documents/MyDocument.pdf'
# s.getfile(file_url, 'downloaded_file.pdf')
# print(f"Downloaded {file_url} to downloaded_file.pdf")
except sharepy.errors.AuthError as e:
print(f"Authentication failed: {e}")
except Exception as e:
print(f"An error occurred: {e}")