Skip to main content

This package contains API wrappers to simplify interaction with Microsoft Graph API through Python functions.

Project description

msgraph-py

Description

This package contains API wrappers to simplify interaction with Microsoft Graph API through Python functions.

Some of the benefits of msgraph-py are:

  • Automatic caching and renewal of access tokens, avoiding unnecessary API-calls.
  • Sets the correct headers and parameters for you when required (advanced queries).
  • Pages results automatically when retrieving large datasets.
  • Useful logging and error messages with the Python logging module.
  • Optional integration with Django settings.py for reading environment variables.

[!NOTE]
The latest published version of this package can be found at pypi.org/project/msgraph-py

List of available functions

Identity

Groups

Devices

Mail

Getting Started

  1. Create an app registration in Entra ID with the necessary Graph application permissions for the functions you intend to use:
    Authentication and authorization steps

  2. Install the latest version of the package:

    python3 -m pip install msgraph-py
    
  3. Configure environment variables:

    • If used within a Django project, msgraph-py will by default first attempt to load the following variables from the project's settings.py:

      # project/settings.py
      
      AAD_TENANT_ID = "00000000-0000-0000-0000-000000000000"
      AAD_CLIENT_ID = "00000000-0000-0000-0000-000000000000"
      AAD_CLIENT_SECRET = "client-secret-value"
      
    • Alternatively you will need to set the following key-value pairs in os.environ:

      import os
      
      os.environ["AAD_TENANT_ID"] = "00000000-0000-0000-0000-000000000000"
      os.environ["AAD_CLIENT_ID"] = "00000000-0000-0000-0000-000000000000"
      os.environ["AAD_CLIENT_SECRET"] = "client-secret-value"
      

[!WARNING]
You should never store sensitive credentials or secrets in production code or commit them to your repository. Always load them at runtime from a secure location or from a local file excluded from the repository.

Usage examples

Get a single user by objectId or userPrincipalName

from msgraph import get_user

user = get_user("user@example.com")

List of returned properties for user resource type.

Get a list of users using advanced query parameters

from msgraph import get_user

filtered_users = get_user(
    filter="startsWith(department, 'sales')",
    select=[
        "displayName",
        "department",
        "createdDateTime",
    ],
    orderby="createdDateTime desc",
    all=True,
)

List of returned properties for user resource type.

Get a users Entra ID joined devices

from msgraph import list_owned_devices

user_devices = list_owned_devices(
    user_id="user@example.com",
    filter="isManaged eq true and trustType eq 'AzureAd'",
    select=[
        "deviceId",
        "displayName",
        "isCompliant",
        "approximateLastSignInDateTime",
    ],
    orderby="approximateLastSignInDateTime desc",
)

List of returned properties for device resource type.

Send an e-mail with attachments

from msgraph import send_mail

send_mail(
    sender_id="noreply@example.com",
    recipients=[
        "john.doe@example.com",
        "jane.doe@example.com",
    ],
    subject="Mail from Graph API",
    body="<h1>Content of the mail body</h1>",
    is_html=True,
    priority="high",
    attachments=[
        "/path/to/file1.txt",
        "/path/to/file2.txt",
    ],
)

API documentation

Resource types and properties

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msgraph_py-1.3.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

msgraph_py-1.3.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file msgraph_py-1.3.0.tar.gz.

File metadata

  • Download URL: msgraph_py-1.3.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for msgraph_py-1.3.0.tar.gz
Algorithm Hash digest
SHA256 2a859b6fdbec4ef85b24750b8cd43e34be93e1ea90e9fb95e44ad27ae5796d16
MD5 bd718a365a631e14817ff31dd21474b1
BLAKE2b-256 edcca24cae4e2524920aef43a793e0df62348cb49f1fa33ae6f546c539bad797

See more details on using hashes here.

Provenance

The following attestation bundles were made for msgraph_py-1.3.0.tar.gz:

Publisher: release.yml on fedamerd/msgraph-py

Attestations:

File details

Details for the file msgraph_py-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: msgraph_py-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for msgraph_py-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 744446ec5500ab25c4053c46f8d7e07af5a9b17f44e7faaf1fbe4846ad09445f
MD5 4c6b9e4f44be8c2ae1f3e1ea45f18483
BLAKE2b-256 08db3e12e66e366af2773669d1ad8d882cd4f8969faade173d94427b67c39159

See more details on using hashes here.

Provenance

The following attestation bundles were made for msgraph_py-1.3.0-py3-none-any.whl:

Publisher: release.yml on fedamerd/msgraph-py

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page