• 【Azure Developer】使用 Powershell az account getaccesstoken 命令获取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.
  • 相关阅读:
    PAT 甲级 1002 A+B for Polynomials (25 分)
    数字提取——C语言
    粒子动画——Pygame
    李白打酒——C++
    C语言输出格雷码
    特殊篮子问题——C语言暴力破解
    黑白迷宫问题——深度优先搜索算法
    最近素数问题——C语言
    巧克力分配问题——C语言
    离散数学——逻辑推理系统
  • 原文地址:https://www.cnblogs.com/lulight/p/16230157.html
Copyright © 2020-2023  润新知