The Microsoft Graph Python SDK
Project description
Microsoft Graph 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 v1.0 of Microsoft Graph. If you want to try the latest Microsoft Graph APIs, try the Beta SDK.
Note: the Microsoft Graph Python SDK is currently in public preview.
1. Installation
pip install msgraph-sdk-python
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: 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
ortrio
. For authentication you need to use one of the async credential classes fromazure.identity
.
from azure.identity.aio import EnvironmentCredential
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
credential=EnvironmentCredential()
auth_provider = AzureIdentityAuthenticationProvider(credential)
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.graph_request_adapter 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.graph_request_adapter import GraphRequestAdapter
from msgraph.core.graph_client_factory 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.graph_service_client 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.
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.graph_request_adapter import GraphRequestAdapter
from msgraph.graph_service_client import GraphServiceClient
credential = ClientSecretCredential(
'tenant_id',
'client_id',
'client_secret'
)
auth_provider = AzureIdentityAuthenticationProvider(credential)
request_adapter = GraphRequestAdapter(auth_provider)
client = GraphServiceClient(request_adapter)
user = asyncio.run(client.users_by_id('userPrincipalName').get())
print(user.display_name)
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.graph_request_adapter import GraphRequestAdapter
from msgraph.graph_service_client import GraphServiceClient
credential = AuthorizationCodeCredential(
'tenant_id',
'client_id',
'authorization_code',
'redirect_uri',
)
auth_provider = AzureIdentityAuthenticationProvider(credential)
request_adapter = GraphRequestAdapter(auth_provider)
client = GraphServiceClient(request_adapter)
user = asyncio.run(client.me().get())
print(user.display_name)
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
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
Built Distribution
File details
Details for the file msgraph-sdk-1.0.0a0.tar.gz
.
File metadata
- Download URL: msgraph-sdk-1.0.0a0.tar.gz
- Upload date:
- Size: 4.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9d4584c824e1423615b13da32be52bec57153599ba43aad57e08f0968fd09f8 |
|
MD5 | 6ab367331d07eef99e3b14d7b5690f5d |
|
BLAKE2b-256 | 82510c3521a8858a3a1986c091e3a9d0613297fb480735c1396a4b7c7ecab040 |
File details
Details for the file msgraph_sdk-1.0.0a0-py3-none-any.whl
.
File metadata
- Download URL: msgraph_sdk-1.0.0a0-py3-none-any.whl
- Upload date:
- Size: 19.6 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34cf035952e3a0f26f82b3947d42037f7198228546440a472401bd7a30efc508 |
|
MD5 | ab12e48baa3ebc2e670664b281603595 |
|
BLAKE2b-256 | ed69536b53389a7fe15cfb1fc1cbb90c3e7b861b205e67ed07256e089b2ce268 |