Skip to content

'az account get-access-token' equivalent in Azure PowerShell #7752

@TylerLeonhardt

Description

@TylerLeonhardt

Right now the Azure Functions CLI relies on Azure PowerShell in order to get an access token to interact with the Azure API:

https://github.com/Azure/azure-functions-core-tools/blob/60ea24fd4408ea876882324a9505c2d43bb37726/src/Azure.Functions.Cli/Actions/AzureActions/BaseAzureAction.cs#L72-L96

I opened an issue on them to change it but they consider it by design because of how complicated it is to get an access token.

Apparently there was an attempt to get this to work in Azure PowerShell as well but unfortunately, there was no way to retrieve the access token needed.

in Azure CLI, it's simple. All you do it:

az account get-access-token

I noticed that on the context object that comes back from Get-AzContext there is a property called Account which has an AccessToken property that is not set:

PS > (Get-AzContext).Account


Id                    : xxxxxxxx
Type                  : User
Tenants               : {xxxxxxxxxxxxxxxxxxx}
AccessToken           :
Credential            :
TenantMap             : {}
CertificateThumbprint :
ExtendedProperties    : {[Subscriptions, xxxxxxxxxxxx]
                        , [Tenants, xxxxxxxxxx]}



PS > (Get-AzContext).Account.AccessToken
PS >

Can this AccessToken property be set so that a user doesn't have to rely on Azure CLI to get it?

I also tried this:

$context = Get-AzContext; $context.TokenCache.ReadItems() | ? TenantId -eq $context.TenantId | % { $_.AccessToken }

but noticed that all of my tokens in the TokenCache were already expired.

Metadata

Metadata

Assignees

Labels

Authenticationfeature-requestThis issue requires a new behavior in the product in order be resolved.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions