You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Azure CLI获取Azure API Management的产品及默认订阅密钥

解决方案

Azure CLI 原生 az apim 命令集目前没有内置查询 APIM 订阅密钥的专用子命令,你可以通过 az rest 直接调用 Azure 管理 REST 接口实现在 Ubuntu 环境下的密钥查询,无需额外安装 PowerShell 模块:

操作步骤

  1. 配置基础参数并指定当前操作的Azure订阅(流水线场景可通过服务连接自动完成CLI认证,无需手动执行登录操作)
# 替换为实际参数
subscriptionId="你的Azure订阅ID"
RG="APIM所属资源组名称"
service="APIM实例名称"

az account set --subscription $subscriptionId
  1. 获取默认(master)订阅密钥
az rest --method post --uri "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$RG/providers/Microsoft.ApiManagement/service/$service/subscriptions/master/listSecrets?api-version=2023-05-01-preview"

执行后返回结果中包含的 primaryKey、secondaryKey 字段即为默认订阅的两组密钥。
3. 获取指定产品关联的订阅密钥
首先查询当前APIM实例下所有订阅记录,定位目标产品对应的订阅ID:

az rest --method get --uri "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$RG/providers/Microsoft.ApiManagement/service/$service/subscriptions?api-version=2023-05-01-preview"

将上一步listSecrets接口路径中的master替换为查询得到的目标订阅ID,即可拉取对应产品订阅的密钥。

备选方案说明

你提到的Azure DevOps PowerShell任务方案也可正常落地,PowerShell Core支持跨平台运行在Ubuntu代理上,只需提前安装Az.ApiManagement模块即可,两种方案可根据流水线现有配置灵活选择。

内容的提问来源于stack exchange,提问作者prakashrajansakthivel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 02:45:02