|
1 | 1 | # Microsoft Graph Authentication |
2 | 2 |
|
3 | | -GraphClient supports the following authentication flows. |
| 3 | +Picking the right authentication flow depends on **who your app runs as** and |
| 4 | +**where it runs**. Use the chart below to decide, then jump to the example. |
4 | 5 |
|
5 | | -| Flow | Method | File | Notes | |
6 | | -|------|--------|------|-------| |
7 | | -| **Client secret** | `with_client_secret(client_id, secret)` | [`with_client_secret.py`](./with_client_secret.py) | App-only, simplest setup | |
8 | | -| **Certificate** | `GraphClient(tenant).with_client_certificate(client_id, thumbprint, key)` | [`with_client_cert.py`](./with_client_cert.py) | App-only, more secure | |
9 | | -| **Interactive** | `with_token_interactive(client_id)` | [`interactive.py`](./interactive.py) | User + MFA compatible | |
10 | | -| **ROPC** | `with_username_and_password(client_id, username, password)` | [`with_user_creds.py`](./with_user_creds.py) | User, no MFA | |
11 | | -| **National cloud** | `AzureEnvironment.USGovernmentHigh` | [`gcc_high.py`](./gcc_high.py) | GCC, DoD, China | |
| 6 | +--- |
| 7 | + |
| 8 | +## Which flow should I use? |
| 9 | + |
| 10 | +```mermaid |
| 11 | +flowchart TD |
| 12 | + A[Who will sign in?] --> B[An application / daemon] |
| 13 | + A --> C[A user] |
| 14 | +
|
| 15 | + B --> D{Where will it run?} |
| 16 | + D -->|Production| E[Client certificate\nmore secure] |
| 17 | + D -->|Development / simple| F[Client secret\nsimplest setup] |
| 18 | +
|
| 19 | + C --> G{User present to interact?} |
| 20 | + G -->|Yes| H[Interactive auth\nsupports MFA, SSO] |
| 21 | + G -->|No, script or CI| I[ROPC (password grant)\nno MFA, legacy] |
| 22 | +
|
| 23 | + E --> J[with_certificate.py] |
| 24 | + F --> K[with_client_secret.py] |
| 25 | + H --> L[interactive.py] |
| 26 | + I --> M[with_user_creds.py] |
| 27 | +
|
| 28 | + style A fill:#1a73e8,color:#fff |
| 29 | + style B fill:#e8f0fe |
| 30 | + style C fill:#e8f0fe |
| 31 | + style E fill:#fff3cd |
| 32 | + style F fill:#fff3cd |
| 33 | + style H fill:#d4edda |
| 34 | + style I fill:#f8d7da |
| 35 | +``` |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## Flow reference |
| 40 | + |
| 41 | +| Flow | Method | Best for | MFA? | File | |
| 42 | +|------|--------|----------|:----:|------| |
| 43 | +| **Client secret** | `with_client_secret(client_id, secret)` | Daemons, cron jobs, CI/CD — app-only access | — | [`with_client_secret.py`](./with_client_secret.py) | |
| 44 | +| **Client certificate** | `with_certificate(client_id, thumbprint, key)` | Production daemons — app-only, no shared secret | — | [`with_client_cert.py`](./with_client_cert.py) | |
| 45 | +| **Interactive** | `with_token_interactive(client_id)` | Desktop apps, CLI tools — user signed-in | ✅ | [`interactive.py`](./interactive.py) | |
| 46 | +| **ROPC (password)** | `with_username_and_password(client_id, user, pass)` | Automated scripts — user context, no interactivity | ✗ | [`with_user_creds.py`](./with_user_creds.py) | |
| 47 | +| **National cloud** | `AzureEnvironment.USGovernmentHigh` | GCC High, DoD, China — applies to any flow above | varies | [`gcc_high.py`](./gcc_high.py) | |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Quick start |
12 | 52 |
|
13 | 53 | ```python |
14 | 54 | from office365.graph_client import GraphClient |
15 | 55 |
|
16 | | -# Client secret (recommended for app-only) |
| 56 | +# App-only (daemon / background job) — simplest |
17 | 57 | client = GraphClient(tenant="contoso.onmicrosoft.com").with_client_secret( |
18 | | - client_id="your_client_id", client_secret="your_secret" |
| 58 | + client_id="your_client_id", client_secret=*** |
19 | 59 | ) |
20 | 60 |
|
21 | | -# Interactive (MFA-compatible) |
| 61 | +# User-authenticated (desktop app) — MFA compatible |
22 | 62 | client = GraphClient(tenant="contoso.onmicrosoft.com").with_token_interactive( |
23 | 63 | client_id="your_client_id" |
24 | 64 | ) |
25 | 65 | ``` |
26 | 66 |
|
| 67 | +> **Note:** All examples use `tests/` module constants. Replace them with |
| 68 | +> your own values from the [Azure Portal](https://portal.azure.com). |
| 69 | +> App-only flows need admin consent granted in the portal. |
| 70 | +
|
27 | 71 | --- |
28 | 72 |
|
29 | 73 | ## Official docs |
30 | 74 |
|
31 | 75 | - [Microsoft Graph authentication overview](https://learn.microsoft.com/en-us/graph/auth) |
32 | 76 | - [Microsoft identity platform auth flows](https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows) |
| 77 | +- [Choose an auth flow](https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#which-auth-flow-should-i-use) |
0 commit comments