Search code examples
azureapiazure-active-directorysubscriptionazure-rest-api

MS Subcriptions REST API returns empty value


I register a new app to my Azure tenant and then use the Subscription REST API below to get my subscription id. But, it returns an empty value.

Is this a bug of the REST API, or the app is missing some required configurations?

https://learn.microsoft.com/en-us/rest/api/resources/subscriptions/list?tabs=HTTP

enter image description here

enter image description here


Solution

  • I tried to reproduce the same in my environment and got below results:

    I registered one Azure AD application and granted API permission like below:

    enter image description here

    I generated the access token via Postman using below parameters:

    POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
    grant_type:client_credentials
    client_id:<appID>
    client_secret:<secret>
    scope: https://management.azure.com/.default
    

    Response:

    enter image description here

    When I used the above token to get subscriptions, I got same response as you like below:

    GET https://management.azure.com/subscriptions?api-version=2020-01-01
    Authorization: Bearer <token>
    

    Response:

    enter image description here

    To get the desired results, make sure to assign required role like Reader to the service principal under your subscription like below:

    Go to Azure Portal -> Subscriptions -> Your Subscription -> Access control (IAM) -> Add role assignment

    enter image description here

    Now I generated token again and got the subscription details like ID successfully with below API call:

    GET https://management.azure.com/subscriptions?api-version=2020-01-01
    Authorization: Bearer <token>
    

    Response:

    enter image description here

    If you want to list all subscriptions, then assign Reader role to your service principal under management group level instead of specific subscription.