Skip to main content

Microsoft Python client

Python tests Pre-commit CodeQL codecov

Build PyPI - Version PyPI - Download PyPI - Python Version

Microsoft graph API Python client with support for Device Flow Oauth tokens with deleguated access.

Install

This package is available on Pypi

pip install ms-python-client

Requirements

  • Python >= 3.9

How to configure the client variables to make API calls

Defining your env variables

Define the following variables in your env or your .env file:

  • AZURE_AUTHORITY
  • AZURE_CLIENT_ID
  • AZURE_SCOPE

For testing purposes

For testing purposes, you can use the following value:

  • MS_ACCESS_TOKEN

This token could be obtained from the Microsoft Graph Explorer by clicking on the Sign in with Microsoft button and then clicking on the Access Token tab.

Usage

Initialize the MSApiClient from environment variables

from ms_python_client import MSApiClient

ms_client = MSApiClient.init_from_env()

Initialize the MSApiClient from .env

from ms_python_client import MSApiClient

ms_client = MSApiClient.init_from_dotenv()

Initialize the MSApiClient manually

from ms_python_client import MSApiClient,  Config

config = Config(
    azure_authority="<YOUR AZURE AUTHORITY>",
    azure_client_id="<YOUR AZURE CLIENT ID>",
    azure_scope="<YOUR AZURE SCOPE>" # Ex. [User.Read, Calendars.ReadWrite, etc.]
)

ms_client = MSApiClient(
    config=config,
)

Store of the cache token

By default the token is stored in a file called token_cache.bin in the current directory. You can change this behavior by passing the token_cache_file parameter to the Config constructor.

from ms_python_client import MSApiClient, Config

config = Config(
    azure_authority="<YOUR AZURE AUTHORITY>",
    azure_client_id="<YOUR AZURE CLIENT ID>",
    azure_scope="<YOUR AZURE SCOPE>", # Ex. [User.Read, Calendars.ReadWrite, etc.]
    token_cache_file="/path/to/token_cache.bin"
)

ms_client = MSApiClient(
    config=config,
)

This will allow you to reuse the token in the next executions of your script and even in different scripts.

How to make API calls

USER_ID = "12345"
SUBJECT = "Test meeting"

query = {"$count": "true", "$filter": f"contains(subject,'{SUBJECT}')"}
result = cern_ms_client.events.list_events(USER_ID, query)

Optional: How to configure the logging

from ms_python_client import setup_logs

setup_logs(log_level=logging.DEBUG)

Available endpoints

events:

  1. get all events
  2. get a single event
  3. create an event
  4. update an event
  5. delete an event

users:

  1. get all users

CERN specific usage

Instead of using the MSApiClient class, you can use the CERNMSApiClient class, which is a subclass of the MSApiClient class. This class will provide you some more utilities but it will only work for CERN users (obviously).

This will be used in the context of synchronizing the events between the CERN Indico system and the calendars of the Zoom Rooms.

How to initialize the CERNMSApiClient

Follow the How to configure the client variables to make API calls section and then:

from ms_python_client import CERNMSApiClient

cern_ms_client = CERNMSApiClient.init_from_dotenv()

Available endpoints

events:

  1. get all events
  2. get a single event using zoom id
  3. create an event
  4. update an event using zoom id
  5. delete an event using zoom id
  6. get the zoom id of an event
  7. get current event of a user

You will find useful the EventParameters and PartialEventParameters classes, which will help you to create the events.

The EventParameters class is used to create an event, while the PartialEventParameters class is used to update an event.

  • ZOOM_ID is the id of the zoom meeting, which can be found inside the url of a meeting link. This is mandatory to create an event.
  • USER_ID is the email of the Zoom Room.
from ms_python_client import CERNMSApiClient, EventParameters, PartialEventParameters

cern_ms_client = CERNMSApiClient.init_from_dotenv()

USER_ID = os.getenv("USER_ID") # Which is the email of the Zoom Room
ZOOM_ID = os.getenv("ZOOM_ID")

event_parameters = EventParameters(
        subject="Test meeting",
        start_time="2021-10-01T12:00:00",
        end_time="2021-10-01T13:00:00",
        timezone="Europe/Zurich",
        zoom_url="https://cern.zoom.us/******",
)

partial_event_parameters = PartialEventParameters(
        end_time="2021-10-01T14:00:00",
) # You can update only the end_time of the event for example

cern_ms_client.events.create_event(USER_ID, ZOOM_ID, event_parameters)
cern_ms_client.events.update_event_by_zoom_id(USER_ID, ZOOM_ID, partial_event_parameters)
cern_ms_client.events.delete_event_by_zoom_id(USER_ID, ZOOM_ID)

Metadata

Release files for ms-python-client 2.0.4

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

Source distribution (sdist)

Source distribution for ms-python-client 2.0.4
File Size Uploaded
ms_python_client-2.0.4.tar.gz 13.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ms-python-client 2.0.4
File Interpreter ABI Platform
ms_python_client-2.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 30.4 kB

Release files / ms_python_client-2.0.4.tar.gz

Download URL ms_python_client-2.0.4.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2e94c2ab36b0f31b6445344c0428f3f77cd6de262f6812b3c7e8e194e026bcf2
BLAKE2b-256 checksum
How to use checksums
7401969934c9ad7df4bb682bc5ee92df8702fb18f883d33d7e1770a3e05f3338
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.0 Linux/5.15.0-1041-azure

Release files / ms_python_client-2.0.4-py3-none-any.whl

Download URL ms_python_client-2.0.4-py3-none-any.whl
Size 17.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7c6379ef2f6fe1e7387c4a0579ee3cae7a837acf01f78482ba270781368b20c
BLAKE2b-256 checksum
How to use checksums
0fc19a388ef1c4339f552e55a854e0e0d1462b83489513af6531ede4d392711b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.0 Linux/5.15.0-1041-azure

Release history Release notifications | RSS feed

This release

2.0.4 This release

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.4

2 release files

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