【Azure Developer】使用 Powershell az account get-access-token 命令获取Access Token (使用用户名+密码)

问题描述

在上篇的文章中,我们使用了JAVA SDK,根据用户名和密码来获取Azure AD的Access Token,这节,我们将使用Powershell az 命令来获取Access Token。

前文回顾:

  1. MASL4J 获取 Token:https://www.cnblogs.com/lulight/p/16226211.html
  2. ADAL4J 获取 Token:https://www.cnblogs.com/lulight/p/16212275.html

PowerShell中使用的AZ命令为: az account get-access-token 

 

问题解答

使用 az account get-access-token之前,需要设定az命令登录环境为中国区Azure

然后,使用az login命令,输入用户名和密码进行登录

最后,使用get-access-token来获取 Token

命令如下:

az cloud set --name AzureChinaCloud

az login 

az account get-access-token --resource https://microsoftgraph.chinacloudapi.cn 

如果az login 没有使用-u -p参数,则会弹出页面让您输入登录用户名和密码,而使用参数,则可以实现静默方式登录。也可以添加 --debug参数查看消息的输出日志:

az cloud set --name AzureChinaCloud
  
az login -u xxxxxxxx@xxxx.xxxxx.com -p xxxxxxxx! --debug 

az account get-access-token --resource https://microsoftgraph.chinacloudapi.cn --debug

以上登录命令的测试效果为:

 

参考资料

az account get-access-token:https://docs.azure.cn/zh-cn/cli/account?view=azure-cli-latest#az-account-get-access-token

Get a token for utilities to access Azure.

The token will be valid for at least 5 minutes with the maximum at 60 minutes. If the subscription argument isn't specified, the current account is used.
az account get-access-token [--resource]
                            [--resource-type {aad-graph, arm, batch, data-lake, media, ms-graph, oss-rdbms}]
                            [--subscription]
                            [--tenant]
Optional Parameters
--resource
Azure resource endpoints. Default to Azure Resource Manager.

--resource-type
Type of well-known resource.

accepted values: aad-graph, arm, batch, data-lake, media, ms-graph, oss-rdbms
--subscription -s
Name or ID of subscription.

--tenant -t
Tenant ID for which the token is acquired. Only available for user and service principal account, not for MSI or Cloud Shell account.

 

posted @ 2022-05-06 19:35  路边两盏灯  阅读(229)  评论(0编辑  收藏  举报