Skip to main content

The Microsoft Graph Beta Python SDK

Project description

Microsoft Graph Beta SDK for Python

PyPI version Downloads Supported Versions Contributors

Get started with the Microsoft Graph Beta 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.

1. Installation

pip install msgraph-beta-sdk

Note:

  • The Microsoft Graph Beta SDK for Python is a fairly large package. It may take a few minutes for the initial installation to complete.
  • Enable long paths in your environment if you receive a Could not install packages due to an OSError. For details, see Enable Long Paths in Windows 10, Version 1607, and Later.

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.3 Get a GraphServiceClient object

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

An instance of the GraphServiceClient class handles building client. To create a new instance of this class, you need to provide an instance of Credential, 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 msgraph_beta import GraphServiceClient

scopes = ['User.Read', 'Mail.Read']
credential = EnvironmentCredential()
client = GraphServiceClient(credential, scopes=scopes)

Note: Refer to the following documentation page if you need to configure an HTTP proxy.

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 msgraph_beta import GraphServiceClient

credential = ClientSecretCredential(
    'tenant_id',
    'client_id',
    'client_secret'
)
scopes = ['https://graph.microsoft.com/.default']
client = GraphServiceClient(credential, scopes=scopes)

async def get_user():
    user = await client.users.by_user_id('userPrincipalName').get()
    if user:
      print(user.display_name)
asyncio.run(get_user())

Note that to calling me requires a signed-in user and therefore delegated permissions. See Authenticating Users) for more:

import asyncio
from azure.identity import InteractiveBrowserCredential
from msgraph_beta import GraphServiceClient

credential = InteractiveBrowserCredential()
scopes=['User.Read']
client = GraphServiceClient(credential, scopes=scopes)

async def me():
    me = await client.me.get()
    if me:
        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.

from kiota_abstractions.api_error import APIError
async def get_user():
    try:
        user = await client.users.by_user_id('userID').get()
        print(user.user_principal_name, user.display_name, user.id)
    except APIError 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


Release history Release notifications | RSS feed

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.60.1.tar.gz (12.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

msgraph_beta_sdk-1.60.1-py3-none-any.whl (49.1 MB view details)

Uploaded Python 3

File details

Details for the file msgraph_beta_sdk-1.60.1.tar.gz.

File metadata

  • Download URL: msgraph_beta_sdk-1.60.1.tar.gz
  • Upload date:
  • Size: 12.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: RestSharp/106.13.0.0

File hashes

Hashes for msgraph_beta_sdk-1.60.1.tar.gz
Algorithm Hash digest
SHA256 f6be4562e97009ff8bb4c4c4edbf1c0ec3e7e202a8b58b259fe0443534fb81d2
MD5 12ba74446de72bb9fe343035d42c5892
BLAKE2b-256 4291294755b703140248085fab091800a6de0cfe16045b06e4a618a512beee4a

See more details on using hashes here.

File details

Details for the file msgraph_beta_sdk-1.60.1-py3-none-any.whl.

File metadata

File hashes

Hashes for msgraph_beta_sdk-1.60.1-py3-none-any.whl
Algorithm Hash digest
SHA256 01cfbaedbabbda96d456ada46cc75aa8d6f1db08a36611deb41def2fff66090f
MD5 bf29fa65f7a456a048c23aaa397e84d6
BLAKE2b-256 41700e45a76753d495d221530e57265f93389d5e8867c8ace286aacab330a4ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page