Skip to content

Commit f77be8c

Browse files
authored
Merge pull request #231168 from Dickson-Mwendia/msal-python-error-handling
[msid][github-issue]Add an example of error handling in MSAL for Python
2 parents 5d320e5 + 71b376a commit f77be8c

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

articles/active-directory/develop/msal-error-handling-python.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.service: active-directory
99
ms.subservice: develop
1010
ms.topic: conceptual
1111
ms.workload: identity
12-
ms.date: 11/26/2020
12+
ms.date: 03/16/2023
1313
ms.author: dmwendia
1414
ms.reviewer: saeeda, rayluo
1515
ms.custom: aaddev
@@ -25,9 +25,34 @@ In MSAL for Python, most errors are conveyed as a return value from the API call
2525
* A successful response contains the `"access_token"` key. The format of the response is defined by the OAuth2 protocol. For more information, see [5.1 Successful Response](https://tools.ietf.org/html/rfc6749#section-5.1)
2626
* An error response contains `"error"` and usually `"error_description"`. The format of the response is defined by the OAuth2 protocol. For more information, see [5.2 Error Response](https://tools.ietf.org/html/rfc6749#section-5.2)
2727

28-
When an error is returned, the `"error_description"` key contains a human-readable message; which in turn typically contains a Microsoft identity platform error code. For details about the various error codes, see [Authentication and authorization error codes](./reference-aadsts-error-codes.md).
28+
When an error is returned, the `"error"` key contains a machine-readable code. If the `"error"` is, for example, an `"interaction_required"`, you may prompt the user to provide additional information to complete the authentication process. If the `"error"` is `"invalid_grant"`, you may prompt the user to reenter their credentials. The following snippet is an example of error handling in MSAL for Python.
2929

30-
In MSAL for Python, exceptions are rare because most errors are handled by returning an error value. The `ValueError` exception is only thrown when there is an issue with how you are attempting to use the library, such as when API parameter(s) are malformed.
30+
```python
31+
32+
from msal import ConfidentialClientApplication
33+
34+
authority_url = "https://login.microsoftonline.com/your_tenant_id"
35+
client_id = "your_client_id"
36+
client_secret = "your_client_secret"
37+
scopes = ["https://graph.microsoft.com/.default"]
38+
39+
app = ConfidentialClientApplication(client_id, authority=authority_url, client_credential=client_secret)
40+
41+
result = app.acquire_token_silent(scopes=scopes, account=None)
42+
43+
if not result:
44+
result = app.acquire_token_silent(scopes=scopes)
45+
46+
if "access_token" in result:
47+
print("Access token: %s" % result["access_token"])
48+
else:
49+
print("Error: %s" % result.get("error"))
50+
51+
```
52+
53+
When an error is returned, the `"error_description"` key also contains a human-readable message, and there is typically also an `"error_code"` key which contains a machine-readable Microsoft identity platform error code. For more information about the various Microsoft identity platform error codes, see [Authentication and authorization error codes](./reference-aadsts-error-codes.md).
54+
55+
In MSAL for Python, exceptions are rare because most errors are handled by returning an error value. The `ValueError` exception is only thrown when there's an issue with how you're attempting to use the library, such as when API parameter(s) are malformed.
3156

3257
[!INCLUDE [Active directory error handling claims challenges](../../../includes/active-directory-develop-error-handling-claims-challenges.md)]
3358

0 commit comments

Comments
 (0)