Cannot get InteractiveBrowserCredential() to cache credentials
See original GitHub issue- Package Name: azure-identity
- Package Version: 1.7.0
- Operating System: Windows 11
- Python Version: 3.8.0
Describe the bug Using InteractiveBrowserCredential() with enable_persistent_cache=True does not seem to cache the credentials over subsequent runs (even for as little as 30 seconds). When I also specify the authenication_record parameter, it still prompts for credentials in the browse on each run.
To Reproduce Steps to reproduce the behavior:
Run the following python program:
# auth_with_cach_test.py: test out ability to get credentials without having to authenticate each time
import os
from azure.identity import InteractiveBrowserCredential, AuthenticationRecord, TokenCachePersistenceOptions
fn = "cred_cache.txt"
deserialized_record = None
found_cc = os.path.exists(fn)
if found_cc:
with open(fn, "rt") as infile:
cred_json = infile.read()
deserialized_record = AuthenticationRecord.deserialize(cred_json)
print("record read from:", fn)
credential = InteractiveBrowserCredential(enable_persistent_cache=True, authenication_record=deserialized_record)
record = credential.authenticate()
if not found_cc:
# serialize to our cache
cred_json = record.serialize()
with open(fn, "wt") as outfile:
outfile.write(cred_json)
print("record written to:", fn)
Expected behavior The first time the program is run, I expect it to prompt for credentials in the browser. The second time the program is run, I expect it to run to completion without prompting for credentials in the browser, but it always prompts.
Screenshots If applicable, add screenshots to help explain your problem.
Additional context See original issue: #9744. Note: I am looking for the simplest way to get an “auth” to pass to the Azure Workspace.get() method, since it seems to require one with for certain Singularity workspaces. I would prefer, in the following order:
- to just log-in using my Azure portal login cached creds - is there a way to do this yet?
- to have Azure cache my credentials itself (in the cloud, or on my local computer) - is there a way to do this yet?
- to be able to cache some bit of information that I can use to get the “auth” without having to make the user authenticate again The above test program represents the suggested code from #9744, as I interpret it. Did I miss something?
Issue Analytics
- State:
- Created a year ago
- Comments:14 (7 by maintainers)
Thanks to help from @xiangyan99, the below, updated program now works correctly (Azure authentication in browser is only done on first run):
If you don’t mind, could you share your code snippets how you use those credentials?
My guess is you did not specify tenant_id for them. If it is not specified, SharedTokenCacheCredential gives a default one but InteractiveBrowserCredential cannot give a default one hence it prompts you to specify.