Skip to content

Commit 771f90b

Browse files
Add an example of error handling in MSAL for Python
1 parent f90f6ae commit 771f90b

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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_description"` key contains a human-readable message; which in turn typically contains a Microsoft identity platform error code. If the `"error code"`, for example, is `"interaction_required"`, you may prompt the user to provide additional information to complete the authentication process. If the `"error code"` 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 code: %s" % result.get("error_code"))
50+
51+
```
52+
53+
For more information about the various 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)