A small, dependency-light toolkit for authenticating to the Microsoft Graph API (app-only / client credentials flow) and making simple REST calls against it.
Project description
graphkit
A small, dependency-light Python toolkit for authenticating to the Microsoft Graph API using the client credentials flow (app-only auth -- no user sign-in required), plus a thin HTTP client for making calls against it.
This is useful for background scripts, scheduled jobs, or shared-mailbox automations that need to act on Outlook mail, Planner tasks, SharePoint, or any other Graph resource without a human logging in each time.
If this saves you time, consider buying me a coffee.
Features
- OAuth2 client credentials flow via MSAL
- No secrets in code -- credentials are read from environment variables
- A minimal
GraphClientfor GET/POST/PATCH/DELETE against any Graph endpoint - Two worked examples: drafting an email, creating a Planner task
Installation
pip install -r requirements.txt
or, to install as an editable package:
pip install -e .
Azure app registration
You need an Azure AD (Entra ID) app registration before this will work. If you don't already have one:
- Go to portal.azure.com -> Azure Active Directory (or Entra ID) -> App registrations -> New registration.
- Give it a name, leave the default options, and click Register.
- Note down the Application (client) ID and Directory (tenant) ID from the app's Overview page.
- Go to Certificates & secrets -> New client secret. Copy the secret value immediately -- it's only shown once.
- Go to API permissions -> Add a permission -> Microsoft
Graph -> Application permissions, and add whichever
permissions your use case needs, for example:
Mail.ReadWrite(draft/read/send mail as any mailbox)Tasks.ReadWrite(Planner tasks)Group.Read.All(look up Microsoft 365 Groups by name)User.Read.All(look up users by name/UPN)
- Click Grant admin consent for your organisation (this step requires a Global Administrator or Application Administrator role).
Application permissions act on the whole tenant, scoped only by whichever specific mailbox/site/group you address in your API calls -- there's no per-user consent screen for this flow. Only grant the permissions you actually need.
Configuration
Copy .env.example to .env and fill in your values:
cp .env.example .env
GRAPH_TENANT_ID=your-tenant-id
GRAPH_CLIENT_ID=your-client-id
GRAPH_CLIENT_SECRET=your-client-secret
.env is already in .gitignore -- never commit real credentials.
To load .env automatically in your own scripts, install
python-dotenv and add this before importing graphkit:
from dotenv import load_dotenv
load_dotenv()
Alternatively, just set the three variables directly in your shell or
process environment -- graphkit doesn't require python-dotenv, it only
reads os.environ.
Usage
from graphkit import get_token, GraphClient
token = get_token() # reads GRAPH_TENANT_ID / GRAPH_CLIENT_ID / GRAPH_CLIENT_SECRET
graph = GraphClient(token)
me = graph.get("/users/someone@yourdomain.com")
print(me["displayName"])
GraphClient is intentionally minimal -- .get(), .post(),
.patch(), .delete() -- so it works with any Graph endpoint, not
just the ones this package happens to have examples for.
Examples
examples/send_email_draft.py-- create a draft email in a mailbox's Drafts folderexamples/create_planner_task.py-- look up a Microsoft 365 Group, Planner plan, and bucket by name, then create a task in it
Run either directly once you've set up your .env:
python examples/send_email_draft.py
python examples/create_planner_task.py
Testing
pip install pytest
pytest
Tests mock MSAL, so they run offline without real Azure credentials.
Why client credentials and not device code / interactive login?
Client credentials (app-only) auth is the right choice when a script runs unattended -- no browser, no one available to click "allow" on a sign-in prompt. If you need a script to act as a specific signed-in user instead of a shared app identity, you'd want MSAL's device code or interactive flows instead, which this package doesn't cover.
License
MIT -- see LICENSE. Do whatever you like with this; a credit or a Ko-fi tip is appreciated but never required.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file msgraphkit-0.1.0.tar.gz.
File metadata
- Download URL: msgraphkit-0.1.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e493f5ad61be13d63ed6964ea32226693fab9090648bfeda0b4a327b95c96f87
|
|
| MD5 |
3a9fb1d5752872b034cb18e765ec0052
|
|
| BLAKE2b-256 |
899e2325b09fde2634529eb3111ff581d4c74f64e514e29d55650a4bb458db34
|
File details
Details for the file msgraphkit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: msgraphkit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a11e3ae7ccf9c613e073769b49cdeb180ea9cd394cbaeb66894d4b219549296
|
|
| MD5 |
984ec45518cba57f1338f6fc274a489e
|
|
| BLAKE2b-256 |
c0d63ceb4fe4cee00c83d3e85e07355ce4b29fe4d2c04f9a906125b0884b925a
|