【Azure Developer】使用 Powershell az account get-access-token 命令获取Access Token (使用用户名+密码)
问题描述
在上篇的文章中,我们使用了JAVA SDK,根据用户名和密码来获取Azure AD的Access Token,这节,我们将使用Powershell az 命令来获取Access Token。
前文回顾:
- MASL4J 获取 Token:https://www.cnblogs.com/lulight/p/16226211.html
- 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.
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?