Skip to content

Commit 6ef32eb

Browse files
authored
Merge pull request #227776 from pamelafox/ad-quickstart
Update the Python quickstart for Azure AD
2 parents 5c81628 + 169d541 commit 6ef32eb

File tree

2 files changed

+58
-68
lines changed

2 files changed

+58
-68
lines changed

articles/active-directory/develop/includes/web-app/quickstart-python.md

Lines changed: 58 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,112 +14,99 @@ ms.author: cwerner
1414
ms.custom: aaddev, devx-track-python, scenarios:getting-started, languages:Python
1515
---
1616

17-
In this quickstart, you download and run a code sample that demonstrates how a Python web application can sign in users and get an access token to call the Microsoft Graph API. Users with a personal Microsoft Account or an account in any Azure Active Directory (Azure AD) organization can sign into the application.
17+
In this quickstart, you download and run a code sample that demonstrates how a Python web application can sign in users and call the Microsoft Graph API. Users with a personal Microsoft Account or an account in any Azure Active Directory (Azure AD) organization can sign into the application.
1818

19-
See [How the sample works](#how-the-sample-works) for an illustration.
20-
21-
## Prerequisites
22-
23-
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
24-
- [Python 2.7+](https://www.python.org/downloads/release/python-2713) or [Python 3+](https://www.python.org/downloads/release/python-364/)
25-
- [Flask](http://flask.pocoo.org/), [Flask-Session](https://pypi.org/project/Flask-Session/), [requests](https://requests.readthedocs.io/en/latest/)
26-
- [MSAL Python](https://github.com/AzureAD/microsoft-authentication-library-for-python)
19+
The following diagram displays how the sample app works:
2720

21+
![Diagram that shows how the sample app generated by this quickstart works.](../../media/quickstart-v2-python-webapp/topology.png)
2822

29-
## Register and download your quickstart app
23+
1. The application uses the [`identity` package](https://pypi.org/project/identity/) to obtain an access token from the Microsoft Identity platform.
24+
2. The access token is used as a bearer token to authenticate the user when calling the Microsoft Graph API.
3025

31-
You have two options to start your quickstart application: express (Option 1), and manual (Option 2)
3226

33-
### Option 1: Register and auto configure your app and then download your code sample
34-
35-
1. Go to the <a href="https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/applicationsListBlade/quickStartType/PythonQuickstartPage/sourceType/docs" target="_blank">Azure portal - App registrations</a> quickstart experience.
36-
1. Enter a name for your application and select **Register**.
37-
1. Follow the instructions to download and automatically configure your new application.
27+
## Prerequisites
3828

39-
### Option 2: Register and manually configure your application and code sample
29+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
30+
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see [how to get an Azure AD tenant.](/azure/active-directory/develop/quickstart-create-new-tenant)
31+
- [Python 3.7+](https://www.python.org/downloads/)
4032

41-
#### Step 1: Register your application
33+
## Step 1: Register your application
4234

43-
To register your application and add the app's registration information to your solution manually, follow these steps:
35+
Follow these steps to register your application in the Azure portal:
4436

4537
1. Sign in to the <a href="https://portal.azure.com/" target="_blank">Azure portal</a>.
4638
1. If you have access to multiple tenants, use the **Directory + subscription** filter :::image type="icon" source="../../media/common/portal-directory-subscription-filter.png" border="false"::: in the top menu to select the tenant in which you want to register an application.
47-
1. Under **Manage**, select **App registrations** > **New registration**.
48-
1. Enter a **Name** for your application, for example `python-webapp` . Users of your app might see this name, and you can change it later.
39+
1. Navigate to the portal's [App registrations](https://go.microsoft.com/fwlink/?linkid=2083908) page, and select **New registration**.
40+
1. Enter a **Name** for your application, for example *python-webapp*.
4941
1. Under **Supported account types**, select **Accounts in any organizational directory and personal Microsoft accounts**.
42+
1. Under **Redirect URIs**, select **Web** for the platform.
43+
1. Enter a redirect URI of `http://localhost:5000/getAToken`. This can be changed later.
5044
1. Select **Register**.
45+
46+
## Step 2: Add a client secret
47+
5148
1. On the app **Overview** page, note the **Application (client) ID** value for later use.
52-
1. Under **Manage**, select **Authentication**.
53-
1. Select **Add a platform** > **Web**.
54-
1. Add `http://localhost:5000/getAToken` as **Redirect URIs**.
55-
1. Select **Configure**.
5649
1. Under **Manage**, select the **Certificates & secrets** and from the **Client secrets** section, select **New client secret**.
57-
1. Type a key description (for instance app secret), leave the default expiration, and select **Add**.
58-
1. Note the **Value** of the **Client Secret** for later use.
50+
1. Enter a description for the client secret, leave the default expiration, and select **Add**.
51+
1. Save the **Value** of the **Client Secret** in a safe location. You'll need it to configure the code, and you can't retrieve it later.
52+
53+
## Step 3: Add a scope
54+
5955
1. Under **Manage**, select **API permissions** > **Add a permission**.
6056
1. Ensure that the **Microsoft APIs** tab is selected.
6157
1. From the *Commonly used Microsoft APIs* section, select **Microsoft Graph**.
62-
1. From the **Delegated permissions** section, ensure that the right permissions are checked: **User.ReadBasic.All**. Use the search box if necessary.
63-
1. Select the **Add permissions** button.
58+
1. From the **Delegated permissions** section, ensure that **User.ReadBasic.All** is selected. Use the search box if necessary.
59+
1. Select **Add permissions**.
6460

65-
#### Step 2: Download your project
61+
## Step 4: Download the sample app
6662

67-
[Download the Code Sample](https://github.com/Azure-Samples/ms-identity-python-webapp/archive/master.zip)
63+
[Download the Python code sample](https://github.com/Azure-Samples/ms-identity-python-webapp/archive/master.zip) or clone the repository:
6864

65+
```powershell
66+
git clone https://github.com/Azure-Samples/ms-identity-python-webapp.git
67+
```
6968

70-
#### Step 3: Configure the Application
69+
You can also use an integrated development environment to open the folder.
7170

72-
1. Extract the zip file to a local folder closer to the root folder - for example, **C:\Azure-Samples**
73-
1. If you use an integrated development environment, open the sample in your favorite IDE (optional).
74-
1. Open the **app_config.py** file, which can be found in the root folder and replace with the following code snippet:
71+
## Step 5: Configure the sample app
7572

76-
```python
77-
CLIENT_ID = "Enter_the_Application_Id_here"
78-
CLIENT_SECRET = "Enter_the_Client_Secret_Here"
79-
AUTHORITY = "https://login.microsoftonline.com/Enter_the_Tenant_Name_Here"
80-
```
81-
Where:
73+
1. Go to the application folder.
8274

83-
- `Enter_the_Application_Id_here` - is the Application ID for the application you registered.
84-
- `Enter_the_Client_Secret_Here` - is the **Client Secret** you created in **Certificates & Secrets** for the application you registered.
85-
- `Enter_the_Tenant_Name_Here` - is the **Directory (tenant) ID** value of the application you registered.
75+
1. Create a *.env* file in the root folder of the project using *.env.sample* as a guide.
8676

87-
#### Step 4: Run the code sample
77+
```python
78+
TENANT_ID=<tenant id>
79+
CLIENT_ID=<client id>
80+
CLIENT_SECRET=<client secret>
81+
```
8882

89-
1. You will need to install MSAL Python library, Flask framework, Flask-Sessions for server-side session management and requests using pip as follows:
83+
* Set the value of `TENANT_ID` to the **Directory (tenant) ID** of the registered application, also available on the overview page.
84+
* Set the value of `CLIENT_ID` to the **Application (client) ID** for the registered application, available on the overview page.
85+
* Set the value of `CLIENT_SECRET` to the client secret you created in **Certificates & Secrets** for the registered application.
86+
87+
The environment variables are referenced in *app_config.py*, and are kept in a separate *.env* file to keep them out of source control. The provided *.gitignore* file prevents the *.env* file from being checked in.
88+
89+
## Step 6: Run the sample app
90+
91+
1. Create a virtual environment for the app:
92+
93+
[!INCLUDE [Virtual environment setup](<../../../../app-service/includes/quickstart-python/virtual-environment-setup.md>)]
94+
95+
1. Install the requirements using `pip`:
9096

9197
```shell
92-
pip install -r requirements.txt
98+
python3 -m pip install -r requirements.txt
9399
```
94100

95-
2. Run `app.py` from shell or command line:
101+
2. Run the app from the command line, specifying the host and port to match the redirect URI:
96102

97103
```shell
98-
python app.py
104+
python3 -m flask run --host=localhost --port=5000
99105
```
100106

101107
> [!IMPORTANT]
102108
> This quickstart application uses a client secret to identify itself as confidential client. Because the client secret is added as a plain-text to your project files, for security reasons, it is recommended that you use a certificate instead of a client secret before considering the application as production application. For more information on how to use a certificate, see [these instructions](../../active-directory-certificate-credentials.md).
103109

104-
## More information
105-
106-
### How the sample works
107-
![Shows how the sample app generated by this quickstart works](../../media/quickstart-v2-python-webapp/python-quickstart.svg)
108-
109-
### Getting MSAL
110-
MSAL is the library used to sign in users and request tokens used to access an API protected by the Microsoft identity Platform.
111-
You can add MSAL Python to your application using Pip.
112-
113-
```Shell
114-
pip install msal
115-
```
116-
117-
### MSAL initialization
118-
You can add the reference to MSAL Python by adding the following code to the top of the file where you will be using MSAL:
119-
120-
```Python
121-
import msal
122-
```
123110

124111
[!INCLUDE [Help and support](../../../../../includes/active-directory-develop-help-support-include.md)]
125112

@@ -129,3 +116,6 @@ Learn more about web apps that sign in users in our multi-part scenario series.
129116

130117
> [!div class="nextstepaction"]
131118
> [Scenario: Web app that signs in users](../../scenario-web-app-sign-user-overview.md)
119+
120+
> [!div class="nextstepaction"]
121+
> [Scenario: Web app that calls web APIs](../../scenario-web-app-call-api-overview.md)
20.1 KB
Loading

0 commit comments

Comments
 (0)