Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

languages
Python
page_type sample
name Python console application that makes a request to the Graph API via the Device Code flow
description This Python console application uses the device code flow for authentication and then makes a request to Microsoft Graph for the user's profile data.
products
azure
azure-active-directory
ms-graph
urlFragment ms-identity-docs-code-app-device-code-python

Python | console | user sign-in, protected web API access (Microsoft Graph) | Microsoft identity platform

This Python console application authenticates a user via the device code flow, and then makes a request to the Graph API as the authenticated user. The response to the request is printed to the terminal.

$ python3 app.py
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXXXXX to authenticate.
{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
  "businessPhones": ["+1 (999) 5551001"],
  "displayName": "Contoso Employee",
  "givenName": "Contoso",
  "jobTitle": "Worker",
  "mail": "cemployee@contoso.com",
  "mobilePhone": "1 999-555-1001",
  "officeLocation": "Contoso Plaza/F30",
  "preferredLanguage": null,
  "surname": "Employee",
  "userPrincipalName": "contoso_employee@contoso.com",
  "id": "00aa00aa-bb11-cc22-dd33-44ee44ee44ee"
}

Prerequisites

  • A Microsoft Entra tenant and the permissions or role required for managing app registrations in the tenant.
  • Python 3

Setup

1. Register the app

First, complete the steps in Register an application with the Microsoft identity platform to register the application.

Use these settings in your app registration.

App registration
setting
Value for this sample app Notes
Name Python Device Code Flow App Suggested value for this sample.
You can change the app name at any time.
Supported account types Accounts in this organizational directory only (Single tenant) Suggested value for this sample.
Platform type None No redirect URI required; don't select a platform.
Allow public client flows Yes Required value for this sample.

ℹ️ Bold text in the tables above matches (or is similar to) a UI element in the Microsoft Entra admin center, while code formatting indicates a value you enter into a text box in the Microsoft Entra admin center.

2. Update code sample with app registration values

# Full directory URL, in the form of https://login.microsoftonline.com/<tenant>
"authority": "",

# 'Application (client) ID' of app registration in Microsoft Entra admin center - this value is a GUID
"client_id": ""

3. Install package(s)

To install MSAL libraries:

pip install -r requirements.txt

Run the application

python3 app.py

Follow the device code flow instructions that are presented. If everything worked, you should receive a response similar to this:

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
  "businessPhones": ["+1 (999) 5551001"],
  "displayName": "Contoso Employee",
  "givenName": "Contoso",
  "jobTitle": "Worker",
  "mail": "cemployee@contoso.com",
  "mobilePhone": "1 999-555-1001",
  "officeLocation": "Contoso Plaza/F30",
  "preferredLanguage": null,
  "surname": "Employee",
  "userPrincipalName": "contoso_employee@contoso.com",
  "id": "00aa00aa-bb11-cc22-dd33-44ee44ee44ee"
}

About the code

This Python console application prompts the user to sign in via their device using a code provided by Microsoft Authentication Library (MSAL). Upon successful authentication, the 'Requests' library then makes an HTTP GET request to the Microsoft Graph /me endpoint with the user's access token in the HTTP header. The response from the GET request is then displayed to the console.

Reporting problems

Sample app not working?

If you can't get the sample working, you've checked Stack Overflow, and you've already searched the issues in this sample's repository, open an issue report the problem.

  1. Search the GitHub issues in the repository - your problem might already have been reported or have an answer.
  2. Nothing similar? Open an issue that clearly explains the problem you're having running the sample app.

All other issues

⚠️ WARNING: Any issue in this repository not limited to running one of its sample apps will be closed without being addressed.

For all other requests, see Support and help options for developers.

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

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.