
Security News
CVE Volume Surges Past 48,000 in 2025 as WordPress Plugin Ecosystem Drives Growth
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.
msgraph-py
Advanced tools
This package contains API wrappers to simplify interaction with Microsoft Graph API through Python functions.
This package contains API wrappers to simplify interaction with Microsoft Graph API through Python functions.
Some of the benefits of msgraph-py are:
[!NOTE]
The latest published version of this package can be found at pypi.org/project/msgraph-py
get_user()get_user_risk()revoke_refresh_tokens()list_auth_methods()delete_auth_method()reset_strong_auth()get_signin()get_group()list_group_members()add_group_member()remove_group_member()get_device()delete_device()list_owned_devices()get_laps_password()send_mail()Create an app registration in Entra ID with the necessary Graph application permissions for the functions you intend to use:
Authentication and authorization steps
Install the latest version of the package:
python3 -m pip install msgraph-py
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.
For improved security, consider migrating to certificate-based authentication instead of a static client secret. The simplest way to do this is using the openssl command to create a self-signed certificate and private key:
hostname=$(hostname -s)
fqdn=$(hostname -f)
openssl req \
-x509 \
-newkey rsa:2048 \
-sha256 \
-days 3650 \
-subj "/CN=${fqdn}" \
-keyout "${hostname}_key.pem" \
-out "${hostname}_cert.pem" \
-noenc \
&> /dev/null
[!NOTE]
Remove the-noencoption if you want to encrypt the private key with a passphrase. Adjust options such as-daysand-subjaccording to your requirements.
Upload the certificate PEM-file to the app registration in Microsoft Entra ID. Make a note of the certificate thumbprint, as we will be needing this in the next step. See Microsofts documentation on adding credentials for more info.
Remove AAD_CLIENT_SECRET from your configuration and set the following environment variables (or settings.py) instead:
os.environ["AAD_PRIVATE_KEY_PATH"] = "path/to/private_key.pem"
# Required if the private key file is password-protected
os.environ["AAD_PRIVATE_KEY_PASSPHRASE"] = "key-passphrase-value"
# Required if the private key file does not contain a X.509 certificate
os.environ["AAD_CERT_THUMBPRINT"] = "cert-thumbprint-value"
[!NOTE]
PKCS#12 format (.pfxor.p12) is also supported in addition to PEM. If the certificate and private key are bundled together in the same file, you may omit settingAAD_CERT_THUMBPRINT, as the thumbprint will be retrieved from the certificate automatically.
from msgraph import get_user
user = get_user("user@example.com")
List of returned properties for user resource type.
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.
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.
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",
],
)
FAQs
This package contains API wrappers to simplify interaction with Microsoft Graph API through Python functions.
We found that msgraph-py demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.