Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
AUaero
Responsive Resident
Responsive Resident

Unexpected results from GetReportSubscriptionsAsAdmin API endpoint

Hello - I hope someone who has already been down this path can help with this issue.

 

I'm using the REST API to get subscriptions for all the reports in my tenant.  I've created a python script that will output the results from the API calls to a CSV.  

 

import requests
from datetime import datetime
import csv
from tqdm import tqdm
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

# Azure Key Vault details
key_vault_url = "https://myKeyVault.vault.azure.net/"
secret_name_client_id = "myClientID"
secret_name_client_secret = "myClientSecret"
secret_name_username = "myUsername"
secret_name_password = "myPassword"

# Authenticate to Azure Key Vault and get Azure AD app credentials
def get_key_vault_secrets():
    credential = DefaultAzureCredential()
    secret_client = SecretClient(vault_url=key_vault_url, credential=credential)
    client_id = secret_client.get_secret(secret_name_client_id).value
    client_secret = secret_client.get_secret(secret_name_client_secret).value
    username = secret_client.get_secret(secret_name_username).value
    password = secret_client.get_secret(secret_name_password).value

    return client_id, client_secret, username, password

# Step 1: Authenticate to Power BI and get Bearer Token
def get_bearer_token(client_id, client_secret, username, password):
    url = "https://login.microsoftonline.com/common/oauth2/token"
    data = {
        'grant_type': 'password',
        'client_id': client_id,
        'client_secret': client_secret,
        'resource': 'https://analysis.windows.net/powerbi/api',
        'username': username,
        'password': password
    }
    response = requests.post(url, data=data)
    return response.json().get('access_token')

# Step 2: Get Group IDs and Names
def get_group_ids_and_names(bearer_token):
    url = "https://api.powerbi.com/v1.0/myorg/groups"
    headers = {'Authorization': f'Bearer {bearer_token}'}
    response = requests.get(url, headers=headers)
    groups_data = response.json()
    group_ids_and_names = [(group['id'], group['name']) for group in groups_data['value']]
    return group_ids_and_names

# Step 3: Get Report IDs and Names for each Group
def get_report_ids_and_names(bearer_token, group_id):
    url = f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports"
    headers = {'Authorization': f'Bearer {bearer_token}'}
    response = requests.get(url, headers=headers)
    reports_data = response.json()
    report_ids_and_names = [(report['id'], report['name']) for report in reports_data['value']]
    return report_ids_and_names

# Step 4: Get Report Subscriptions for Each Report
def get_report_subscriptions(bearer_token, report_id):
    url = f"https://api.powerbi.com/v1.0/myorg/admin/reports/{report_id}/subscriptions"
    headers = {'Authorization': f'Bearer {bearer_token}'}
    response = requests.get(url, headers=headers)
    subscription_data = response.json()
    subscriptions = [(subscription['id'], subscription['artifactDisplayName'], 
        subscription['subArtifactDisplayName'], subscription['frequency'], subscription['startDate'], 
        subscription['endDate'], subscription['users'][0]['identifier'], subscription['users'][0]['displayName'])
        for subscription in subscription_data['value']]
    return subscriptions

# Main Function
def main():
    # Authenticate to Azure Key Vault and get Azure AD app credentials
    client_id, client_secret, username, password = get_key_vault_secrets()

    # Step 1: Authenticate
    bearer_token = get_bearer_token(client_id, client_secret, username, password)

    # Step 2: Get Group IDs and Names
    group_ids_and_names = get_group_ids_and_names(bearer_token)

    # Create CSV file
    csv_filename = "c:/power_bi_subscriptions.csv"
    with open(csv_filename, mode='w', newline='') as csv_file:
        csv_writer = csv.writer(csv_file)
        csv_writer.writerow(["GroupName", "GroupID", "ReportName", "ReportID", "SubscriptionID", "Report", "Page", "Frequency", 
            "StartDate", "EndDate", "UPN", "UserName"])

        # Steps 3 & 4: Iterate through Groups, Datasets, and Refresh History
        for group_id, group_name in group_ids_and_names:
            # Step 3: Get Report IDs and Names
            report_ids_and_names = get_report_ids_and_names(bearer_token, group_id)

            # Step 4: Get Subscriptions for each Report
            for report_id, report_name in tqdm(report_ids_and_names, desc=f"{group_name} ({group_id})", unit="report"):
                # Get Report Subscriptions
                report_subscriptions = get_report_subscriptions(bearer_token, report_id)

                # Write to CSV
                for subscription in report_subscriptions:
                    row_data = [group_name, group_id, report_name, report_id]
                    row_data.extend(subscription)
                    csv_writer.writerow(row_data)

    print(f"Results written to {csv_filename}")

if __name__ == "__main__":
    main()

 

Here's the issue I'm seeing - I can go to a specific workspace in Power BI and view the subscriptions that exist in that workspace.  To test, I selected a single report from a single workspace that definitely has subscriptions.  When I step through my code, the API call for that specific report ID in Step 4: Get Report Subscriptions for Each Report doesn't return anything from the API call.  Similarly, if I go to the API sandbox for that endpoint at Admin - Reports GetReportSubscriptionsAsAdmin - REST API (Power BI Power BI REST APIs) | Microsoft L... and run the API call from there for the same report ID, I get no results there either.  I'm having trouble understanding how I can see subscriptions for that specific report ID when I manage subscriptions in the workspace in the Power BI service, but get no results from the API call.


Here is a screenshot from the PBI service showing a subscription to a report named "Sales Metrics"

AUaero_0-1705959858284.png

 

That particular report has a report ID of 72ac9439-3fa1-4f43-9031-30bb37b3ab20.

 

When I call the API endpoint to get the subscriptions for that report ID, I get this response:

 

{
  "@odata.context": "http://wabi-us-north-central-d-primary-redirect.analysis.windows.net/v1.0/myorg/admin/$metadata#Collection(Microsoft.PowerBI.ServiceContracts.Api.Access.Subscription)",
  "value": []
}

 

My python script returns the same response.

 

Any ideas?

 

Thanks!

2 REPLIES 2
AUaero
Responsive Resident
Responsive Resident

Thanks, @v-yiruan-msft,

Here's the report that I'm expecting to see a subscription from.  The report ID is 4636ca4c-c8fe-4875-81d7-cc7d0293418a.

AUaero_2-1706022000914.png

Here's the subscription that I created for that report:

AUaero_3-1706022119638.png

 

I expect to see six subscriptions for this report based on what I see in Power BI:

AUaero_4-1706022188102.png

When I make a call to the GetReportSubscriptionsAsAdmin for report ID 4636ca4c-c8fe-4875-81d7-cc7d0293418a, I don't get any susbcription information in the response:

AUaero_5-1706022380421.png

API Response:

 

{
  "@odata.context": "http://wabi-us-north-central-d-primary-redirect.analysis.windows.net/v1.0/myorg/admin/$metadata#Collection(Microsoft.PowerBI.ServiceContracts.Api.Access.Subscription)",
  "value": []
}

 


Where I get really confused is when I call the GetUserSubscriptionsAsAdmin endpoint for one of the users that has a subscription to the sales metrics report indicated in the screenshot above.  That endpoint indicates a different artifactID for the subcription - 0e601d38-41a1-472f-acab-f0d13795b0ac.

AUaero_7-1706025356211.png


When I call the GetReportsInGroupAsAdmin endpoint for the group ID associated with the workspace where the target report is saved, I verify that the report ID is 4636ca4c-c8fe-4875-81d7-cc7d0293418a

AUaero_6-1706025288909.png

 







v-yiruan-msft
Community Support
Community Support

Hi @AUaero ,

Thanks for reaching out to us with your problem. Base on your description, it seems that it can't return the record when call REST API  Reports GetReportSubscriptionsAsAdmin for the report "Sales Metrics". In order to get a better understanding on your problem and make furthere troubleshooting, could you please provide the following info?

  • Did you get the correct report id for Sales Metrics report which has subscription?
  • Are there multiple reports with the name "Sales Metrics"? The other one named "Sales Metrics"hasn't subscription, and your report id is fetched from that unsubscribed report, soit not return any results when call REST API  Reports GetReportSubscriptionsAsAdmin...
  • Could you please check check if it will return any record when call that rest api use different account?

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.