Skip to content

Latest commit

 

History

History
97 lines (63 loc) · 4 KB

python-azure.md

File metadata and controls

97 lines (63 loc) · 4 KB
title description author ms.author ms.topic date
Build API clients for Python with Microsoft identity authentication
Learn how use Kiota to build API clients in Python to access APIs that require Microsoft identity authentication.
isvargasmsft
isvargas
tutorial
03/20/2023

Build API clients for Python with Microsoft identity authentication

In this tutorial, you generate an API client that uses Microsoft identity authentication to access Microsoft Graph.

Required tools

Create a project

Create a directory to contain the new project.

Add dependencies

Before you can compile and run the generated API client, you need to make sure the generated source files are part of a project with the required dependencies. Your project must have a reference to the abstraction package. Additionally, you must either use the Kiota default implementations or provide your own custom implementations of the following packages.

For this tutorial, use the default implementations.

Run the following commands to get the required dependencies.

pip install microsoft-kiota-abstractions
pip install microsoft-kiota-authentication-azure
pip install microsoft-kiota-http
pip install microsoft-kiota-serialization-json
pip install microsoft-kiota-serialization-text
pip install azure-identity

Tip

It is recommended to use a package manager/virtual environment to avoid installing packages system wide. Read more here.

Generate the API client

Kiota generates API clients from OpenAPI documents. Create a file named get-me.yml and add the following.

:::code language="yaml" source="~/code-snippets/get-started/azure-auth/get-me.yml":::

You can then use the Kiota command line tool to generate the API client classes.

kiota generate -l python -d get-me.yml -c GetUserApiClient -n client -o ./client

Register an application

[!INCLUDE register-azure-app-device-code-intro]

[!INCLUDE register-azure-app-device-code-portal]

[!INCLUDE register-azure-app-device-code-powershell]


Create the client application

Create a file in the root of the project named get_user.py and add the following code. Replace YOUR_CLIENT_ID with the client ID from your app registration.

:::code language="python" source="~/code-snippets/get-started/azure-auth/python/get_user.py" id="ProgramSnippet":::

Note

This example uses the DeviceCodeCredential class. You can use any of the credential classes from the azure.identity library.

Run the application

To start the application, run the following command in your project directory.

python3 get_user.py

See also