Skip to main content

The Microsoft Graph Beta Python SDK

Project description

Microsoft Graph Beta SDK for Python

Get started with the Microsoft Graph SDK for Python by integrating the Microsoft Graph API into your Python application.

Note:

  • This SDK allows you to build applications using the latest beta version of Microsoft Graph. If you want to try the v1.0 Microsoft Graph API, use the v1.0 SDK.
  • The Microsoft Graph Beta Python SDK is currently in public preview. Don't use this SDK in production environments. For details see SDKs in preview or GA status.

1. Installation

pip install msgraph-beta-sdk

2. Getting started with Microsoft Graph

2.1 Register your application

Register your application by following the steps at Register your app with the Microsoft Identity Platform.

2.2 Create an AuthenticationProvider object

An instance of the GraphServiceClient class handles building client. To create a new instance of this class, you need to provide an instance of AuthenticationProvider, which can authenticate requests to Microsoft Graph.

Note: For authentication we support both sync and async credential classes from azure.identity. Please see the azure identity docs for more information.

# Example using async credentials.
from azure.identity.aio import EnvironmentCredential
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider

scopes = ['User.Read', 'Mail.Read']
credential=EnvironmentCredential()
auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes)

2.3 Initialise a GraphRequestAdapter object

The SDK uses an adapter object that handles the HTTP concerns. This HTTP adapter object is used to build the Graph client for making requests.

To initialise one using the authentication provider created in the previous step:

from msgraph import GraphRequestAdapter

adapter = GraphRequestAdapter(auth_provider)

We currently use HTTPX as our HTTP client. You can pass your custom configured httpx.AsyncClient using:

from msgraph import GraphRequestAdapter
from msgraph_core import GraphClientFactory

http_Client = GraphClientFactory.create_with_default_middleware(client=httpx.AsyncClient())
request_adapter = GraphRequestAdapter(auth_Provider, http_client)

2.3 Get a GraphServiceClient object

You must get a GraphServiceClient object to make requests against the service.

from msgraph import GraphServiceClient

client = GraphServiceClient(request_adapter)

3. Make requests against the service

After you have a GraphServiceClient that is authenticated, you can begin making calls against the service. The requests against the service look like our REST API.

Note: This SDK offers an asynchronous API by default. Async is a concurrency model that is far more efficient than multi-threading, and can provide significant performance benefits and enable the use of long-lived network connections such as WebSockets. We support popular python async envronments such as asyncio, anyio or trio.

The following is a complete example that shows how to fetch a user from Microsoft Graph.

import asyncio
from azure.identity.aio import ClientSecretCredential
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
from msgraph import GraphRequestAdapter
from msgraph import GraphServiceClient

credential = ClientSecretCredential(
    'tenant_id',
    'client_id',
    'client_secret'
)
scopes = ['User.Read']
auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes)
request_adapter = GraphRequestAdapter(auth_provider)
client = GraphServiceClient(request_adapter)

async def get_user():
    user = await client.users_by_id('userPrincipalName').get())
    print(user.display_name)

asyncio.run(get_user())

Note that to calling me requires a signed-in user and therefore delegated permissions (obtained using the authorization_code flow):

import asyncio
from azure.identity.aio import AuthorizationCodeCredential
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
from msgraph import GraphRequestAdapter
from msgraph import GraphServiceClient

credential = AuthorizationCodeCredential(
    'tenant_id',
    'client_id',
    'authorization_code',
    'redirect_uri',
)
scopes=['User.Read']
auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes)
request_adapter = GraphRequestAdapter(auth_provider)
client = GraphServiceClient(request_adapter)

async def me():
    me = await client.me.get()
    print(me.display_name)

asyncio.run(me())

3.1 Error Handling

Failed requests raise APIError exceptions. You can handle these exceptions using try catch statements.

async def get_user():
    try:
        user = await client.users_by_id('userID').get()
        print(user.user_principal_name, user.display_name, user.id)
    except Exception as e:
        print(f'Error: {e.error.message}')

asyncio.run(get_user())

Documentation and resources

Upgrading

For detailed information on breaking changes, bug fixes and new functionality introduced during major upgrades, check out our Upgrade Guide

Issues

View or log issues on the Issues tab in the repo.

Contribute

Please read our Contributing guidelines carefully for advice on how to contribute to this repo.

Copyright and license

Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Third Party Notices

Third-party notices

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-beta-sdk-1.0.0a2.tar.gz (10.3 MB view details)

Uploaded Source

Built Distribution

msgraph_beta_sdk-1.0.0a2-py3-none-any.whl (40.3 MB view details)

Uploaded Python 3

File details

Details for the file msgraph-beta-sdk-1.0.0a2.tar.gz.

File metadata

  • Download URL: msgraph-beta-sdk-1.0.0a2.tar.gz
  • Upload date:
  • Size: 10.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for msgraph-beta-sdk-1.0.0a2.tar.gz
Algorithm Hash digest
SHA256 483b57dd22c9b46fc5b7ee185b8c3c55d72cd213d3ba80f2a7196c2b3ad80b3b
MD5 bae7bc106cdf912feb868226a797cc84
BLAKE2b-256 38902f98560e3da461a03b101bd0b6fd86c9b96165939dd2b38d3fb38d2a5fa7

See more details on using hashes here.

File details

Details for the file msgraph_beta_sdk-1.0.0a2-py3-none-any.whl.

File metadata

File hashes

Hashes for msgraph_beta_sdk-1.0.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 2c9e963c7302fcc1a27215a1365ea0ce498eb0ef277c051c7a5707db3360c9a2
MD5 34b1b171837cedf5af9765567b0dd58d
BLAKE2b-256 830ceb3b8ac1fff6165705b20a3d1705ac628e49c50434247f019aa661a77400

See more details on using hashes here.

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