問題描述
在上篇的文章中,我們使用了JAVA SDK,根據(jù)用戶名和密碼來獲取Azure AD的Access Token,這節(jié),我們將使用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命令登錄環(huán)境為中國區(qū)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參數(shù),則會彈出頁面讓您輸入登錄用戶名和密碼,而使用參數(shù),則可以實現(xiàn)靜默方式登錄。也可以添加 --debug參數(shù)查看消息的輸出日志:
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
以上登錄命令的測試效果為:

image.png
參考資料
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.
當在復雜的環(huán)境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 云中,恰是如此!
標簽: Azure Developer, Powershell, az account get-access-token