Coder Social home page Coder Social logo

fedamerd / msgraph-py Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 23 KB

Python package providing API wrappers to simplify interaction with Microsoft Graph API.

Home Page: https://pypi.org/project/msgraph-py

License: MIT License

Python 100.00%
python azuread microsoft-graph microsoft365 msgraph api-wrapper

msgraph-py's Introduction

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("[email protected]")

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="[email protected]",
    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="[email protected]",
    recipients=[
        "[email protected]",
        "[email protected]",
    ],
    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

msgraph-py's People

Contributors

fedamerd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.