@@ -25,10 +25,21 @@ def __init__(self, client_id, resource_id, authority=AUTHORITY_URI, client_crede
2525 self .app = msal .PublicClientApplication (self .client_id , authority = AUTHORITY_URI ,
2626 client_credential = self .client_credentials , token_cache = self .cache )
2727 self .accounts = self .app .get_accounts ()
28- self ._oauth_get_token_silent () if self ._cache_available () else self ._oauth_device_code ()
2928
29+ if self ._cache_available ():
30+ if not self .accounts :
31+ print ("Token cache found but have no accounts" )
32+ else :
33+ print ("Get token and maybe refresh" )
34+ self ._oauth_get_token_silent ()
35+ else :
36+ print ("No token cache found, reauthenticate" )
37+ self ._oauth_device_code ()
38+
39+
3040 def get_token (self ):
3141 if self .is_token_expired ():
42+ print ("Token expired, get new token" )
3243 self ._oauth_get_token_silent ()
3344
3445 return self .result ["access_token" ]
@@ -41,12 +52,21 @@ def is_token_expired(self):
4152
4253 def _oauth_get_token_silent (self ):
4354 if not self .accounts :
55+ print ("Get accounts" )
4456 self .accounts = self .app .get_accounts ()
4557
4658 if not self ._check_token_security ():
4759 raise SystemError ('The token is not stored safely.' )
4860
49- self .result = self .app .acquire_token_silent ([self .scope ], account = self .accounts [0 ])
61+ self .result = self .app .acquire_token_silent_with_error ([self .scope ], account = self .accounts [0 ])
62+
63+ if "access_token" in self .result :
64+ print ("Token found" )
65+ elif "error" in self .result :
66+ print ("Error getting access token" + self .result ["error" ])
67+ else :
68+ print ("Acuire token failed" )
69+
5070 self ._set_expiring_date (int (self .result ['expires_in' ]))
5171 self ._write_cache ()
5272
0 commit comments