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

简介: 【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.

 

相关文章
|
25天前
【Azure 应用服务】Azure Powershell Function 出错 The term 'Connect-AzAccount' is not recognized
【Azure 应用服务】Azure Powershell Function 出错 The term 'Connect-AzAccount' is not recognized
|
22天前
|
监控 安全 数据安全/隐私保护
|
24天前
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
|
24天前
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
|
24天前
【Application Insights】使用Powershell命令向Application Insgihts发送测试数据
【Application Insights】使用Powershell命令向Application Insgihts发送测试数据
|
24天前
|
存储 C# Python
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
|
25天前
|
Ubuntu Linux 测试技术
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
|
25天前
|
数据安全/隐私保护 异构计算 Windows
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter
|
25天前
|
存储 Shell 容器
【Azure 存储服务】使用PowerShell脚本创建存储账号(Storage Account)的共享访问签名(SASToken) : New-AzStorageContainerSASToken
【Azure 存储服务】使用PowerShell脚本创建存储账号(Storage Account)的共享访问签名(SASToken) : New-AzStorageContainerSASToken
|
3天前
|
Windows
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
34 10