简体   繁体   中英

Fetch Azure API Management subscription key using azure cli

I am trying to get the subscription keys for my products as well as default subscription key using cli. I have gone through the documentation https://learn.microsoft.com/en-us/cli/azure/apim/api?view=azure-cli-latest , but right now I don't see any commands to get me the subscription key.

While I can see there are powershell way of getting it, we run the tasks in ubuntu pipeline, and the commands listed below is not working in the linux agent. It says Set-AzContext is not a known command

$subscriptionId = "id"
$RG = "rg"
$service = "apim-name"

Set-AzContext -Subscription $subscriptionId 

$apimContext = New-AzApiManagementContext -ResourceGroupName $RG -ServiceName $service

Get-AzApiManagementSubscriptionKey -Context $apimContext -SubscriptionId "master"

Update I am able to fetch the details through powershell task for Azure in the DevOps pipeline. If there is no option in azure cli I will use this as a workaround.

Using az rest it is possible:

APIMID=`az apim show -n apimname -g resourcegroup --query id -o tsv`
az rest --method post  --uri ${APIMID}/subscriptions/test-subscription/listSecrets?api-version=2021-08-01 --query primaryKey -o tsv

where test-subscription is the name of the subscription.

Currently it is not possible to fetch subscription key using AZ CLI commands. The PowerShell command used is the correct way to go. Just in case if it helps another way to get the subscription key is by using Management API call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM