Skip to main content

DeviceCodeCredential

azure.identity.DeviceCodeCredential with a persisted cache and get_refresh_token().

Usage

Source

DeviceCodeCredential(
    *args,
    allow_unencrypted_storage=False,
    **kwargs,
)

Signs in with a device code: it prints a URL and a code, and the user signs in from any browser, on any machine. Use it where no browser can reach the machine running Python. As a SupportsRefreshToken, it can be passed to spark_storage_conf(refresh_token=...).

Parameters

*args: Any

Passed to azure.identity.DeviceCodeCredential (client_id, which defaults to the Azure CLI’s).

allow_unencrypted_storage: bool = False

Fall back to a plain file where the platform’s secret store is unusable (a headless Linux machine). Pass it inside your own cache_persistence_options instead when you give those.

**kwargs: Any
Passed to azure.identity.DeviceCodeCredential: tenant_id, authority, timeout, prompt_callback, authentication_record and the rest.

Raises

ValueError
allow_unencrypted_storage=True together with cache_persistence_options.

Notes

Everything but the cache default and get_refresh_token() is azure-identity’s. Tokens are cached in the platform store under the name "dazapiz" unless cache_persistence_options is given, so a later session with the same authentication_record signs in silently.

Examples

from dazapiz import DeviceCodeCredential, spark_storage_conf

credential = DeviceCodeCredential(tenant_id="contoso.com")
conf = spark_storage_conf(refresh_token=credential)  # prompts once

Methods

Name Description
get_refresh_token() The signed-in user’s refresh token, signing in first if nobody has.

get_refresh_token()

The signed-in user’s refresh token, signing in first if nobody has.

Usage

Source

get_refresh_token()

Calls authenticate() when the credential has no AuthenticationRecord yet, then reads the token from the persisted cache azure-identity wrote, with the record’s client ID, tenant and username.

Returns
RefreshTokenInfo
The newest refresh token in the cache for the signed-in account.
Raises
RefreshTokenNotFoundError
The persisted cache holds no refresh token for the account, or cannot be read.
Notes

The cache is opened with the same msal_extensions persistence azure-identity uses for its cache_persistence_options, and only loaded: nothing is written or copied. This reads azure-identity’s private _persistent_cache layout, which its public API does not expose.