Skip to main content

az_login()

Log the Azure CLI in with a device code, unless it already is.

Usage

Source

az_login(
    tenant_id=None,
    *,
    force=False,
    timeout=None,
)

Unless force, returns the current account without prompting when the CLI is logged in (into tenant_id, if given). Otherwise runs az login --use-device-code [--tenant <tenant_id>] --output json, prints the device code and URL as soon as az streams them on stderr, copies the code to the clipboard when a clipboard tool is found, waits for az to finish and returns the account it made the default. Prints the device code and the account name, nothing else from the login.

Parameters

tenant_id: str | None = None

Log in to this tenant (GUID or domain); an account logged in to another tenant counts as not logged in. A domain matches the account’s tenantDefaultDomain.

force: bool = False

Log in again even when the CLI already is.

timeout: float | None = None
Seconds to wait for the whole login, the az account show checks included; None, the default, waits.

Returns

dict
The account az made the default, as az_account_show() returns it.

Raises

AzCliNonInteractiveError

Nobody can answer the prompt: no TTY and no notebook kernel.

AzCliTimeoutError

timeout seconds passed first; az is killed.

AzCliNotFoundError

There is no az on PATH.

AzCliError

az login failed.

TypeError

tenant_id is not a string.

ValueError
tenant_id is not a valid tenant ID.

Notes

Runs az login (azr::az_cli_login()); the sign-in itself is the Azure CLI’s. Afterwards refresh_token_from_az_cli() finds the new refresh token.

Examples

from dazapiz import az_login

az_login()  # To sign in, open https://microsoft.com/devicelogin and enter the code ...