Microsoft Graph Beta Python SDK

1.57.0 · active · verified Thu Apr 16

The `msgraph-beta-sdk` is the official Python SDK for interacting with the Microsoft Graph API's beta endpoint. It enables developers to build applications using the latest, often experimental, features of Microsoft Graph. As of its current version (1.57.0), it offers an asynchronous API by default and integrates with `azure.identity` for authentication. It's important to note that this SDK is intended for development and testing new features, not for production environments, due to potential breaking changes and the evolving nature of beta APIs.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `GraphServiceClient` using `EnvironmentCredential` from `azure.identity.aio` for asynchronous operations and fetch the current user's profile. Ensure your application is registered with Azure AD and the necessary environment variables (`AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`) are set, along with appropriate API permissions (scopes).

import asyncio
import os
from azure.identity.aio import EnvironmentCredential
from msgraph_beta import GraphServiceClient

async def main():
    # It's recommended to set 'AZURE_TENANT_ID', 'AZURE_CLIENT_ID', and 'AZURE_CLIENT_SECRET'
    # environment variables for EnvironmentCredential.
    # Replace with your actual scopes based on the permissions required by your application.
    scopes = ['User.Read', 'Mail.Read'] 

    try:
        credential = EnvironmentCredential()
        client = GraphServiceClient(credential, scopes=scopes)

        # Example: Get the currently signed-in user's profile
        user = await client.me.get()
        print(f"Hello, {user.display_name}!")
        print(f"Your email: {user.mail}")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →