33
44import msal
55
6- def authorize_by_username_password (username : str , password : str , * , client_id : str , tenant_id : str ) -> GraphAccess :
7- """Get a bearer token for the given user.
8- This is used in most other Graph API calls.
9-
10- Args:
11- username: The username of the user (email address).
12- password: The password of the user.
13- client_id: The Graph API client id in 8-4-4-12 format.
14- tenant_id: The Graph API tenant id in 8-4-4-12 format.
15-
16- Returns:
17- GraphAccess: The GraphAccess object used to authorize Graph access.
18- """
19- authority = f"https://login.microsoftonline.com/{ tenant_id } "
20- scopes = ["https://graph.microsoft.com/.default" ]
21-
22- app = msal .PublicClientApplication (client_id , authority = authority )
23- app .acquire_token_by_username_password (username , password , scopes )
24-
25- graph_access = GraphAccess (app , scopes )
26-
27- # Test connection
28- graph_access .get_access_token ()
29-
30- return graph_access
31-
32-
6+ # pylint: disable-next=too-few-public-methods
337class GraphAccess :
348 """An object that handles access to the Graph api.
359 This object should not be created directly but instead
@@ -41,7 +15,7 @@ def __init__(self, app: msal.PublicClientApplication, scopes: list[str]) -> str:
4115
4216 def get_access_token (self ):
4317 """Get the access token to Graph.
44- This function automatically reuses an existing token
18+ This function automatically reuses an existing token
4519 or refreshes an expired one.
4620
4721 Raises:
@@ -60,3 +34,30 @@ def get_access_token(self):
6034 raise RuntimeError (f"Token could not be acquired. { token ['error_description' ]} " )
6135
6236 raise RuntimeError ("Something went wrong. No error description was returned from Graph." )
37+
38+
39+ def authorize_by_username_password (username : str , password : str , * , client_id : str , tenant_id : str ) -> GraphAccess :
40+ """Get a bearer token for the given user.
41+ This is used in most other Graph API calls.
42+
43+ Args:
44+ username: The username of the user (email address).
45+ password: The password of the user.
46+ client_id: The Graph API client id in 8-4-4-12 format.
47+ tenant_id: The Graph API tenant id in 8-4-4-12 format.
48+
49+ Returns:
50+ GraphAccess: The GraphAccess object used to authorize Graph access.
51+ """
52+ authority = f"https://login.microsoftonline.com/{ tenant_id } "
53+ scopes = ["https://graph.microsoft.com/.default" ]
54+
55+ app = msal .PublicClientApplication (client_id , authority = authority )
56+ app .acquire_token_by_username_password (username , password , scopes )
57+
58+ graph_access = GraphAccess (app , scopes )
59+
60+ # Test connection
61+ graph_access .get_access_token ()
62+
63+ return graph_access
0 commit comments