Plenty to read!

Plenty to read!

Get Publish-to-Web & Org. share links from Power BI API with Python

Get Publish-to-Web & Org. share links from Power BI API with Python


USE PYTHON TO GET DATA FROM POWER BI APIs

…such as the publish-to-web links created in your organization


 

IN THIS ARTICLE

  1. Get Publish-to-Web share links from Power BI Admin API using Python

  2. Get links shared to the entire organization (i.e. for Reports, Dashboards)

Example result dataframe for publish-to-web reports obtained with Power BI APIs


HOW TO SET IT UP?

I wrote a post about setting up a Service Principal, permissions & settings to get you started, as well as explaining the Authentication. You can find that post, here.


#########################################################################################
# Authentication - Replace string variables with your relevant values       
#########################################################################################

import json, requests, pandas as pd
try:
    from azure.identity import ClientSecretCredential
except Exception:
     !pip install azure.identity
     from azure.identity import ClientSecretCredential

# --------------------------------------------------------------------------------------#
# String variables: Replace with your own
tenant = 'Your-Tenant-ID'
client = 'Your-App-Client-ID'
client_secret = 'Your-Client-Secret-Value' # See Note 2: Better to use key vault
api = 'https://analysis.windows.net/powerbi/api/.default'
# --------------------------------------------------------------------------------------#

# Generates the access token for the Service Principal
auth = ClientSecretCredential(authority = 'https://login.microsoftonline.com/',
                                                        tenant_id = tenant,
                                                        client_id = client,
                                                        client_secret = client_secret)
access_token = auth.get_token(api)
access_token = access_token.token

print('
Successfully authenticated.')   

#########################################################################################

# Note 1: This code was authored for use in a Google Colab Notebook
# Note 2: Check here for info on using Azure Key Vault: 
          # https://docs.microsoft.com/en-us/azure/key-vault/secrets/quick-create-python    
          # The above code should be modified to programmatically get the secret from AKV

#########################################################################################
# Admin: Get all Publish-to-Web links
#########################################################################################

base_url = 'https://api.powerbi.com/v1.0/myorg/'
header = {'Authorization': f'Bearer {access_token}'}

# The admin API doesn't work unless you provide a ?$top=n argument
# Top 5000 workspaces
# Admin: Get all Publish to Web artifacts

# Query for the admin scenario (everything)
# The admin API doesn't work unless you provide a ?$top=n argument

topn = '$top=5000'
admin_pubtoweb = f'{base_url}admin/widelySharedArtifacts/publishedToWeb?{topn}'

# HTTP GET Request
published_to_web = requests.get(admin_pubtoweb, headers=header)

# Response code (200 = Success; 401 = Unauthorized; 404 = Bad Request)
print(published_to_web)
try:
    published_to_web = json.loads(published_to_web.content)
except Exception as e:
    print(e)
    exit()
try:
    pd.concat([pd.json_normalize(x) for x in published_to_web['ArtifactAccessEntities']])
except Exception:
    print('No publish-to-web links found.')

#########################################################################################
# Authentication - Replace string variables with your relevant values       
#########################################################################################

import json, requests, pandas as pd
try:
    from azure.identity import ClientSecretCredential
except Exception:
     !pip install azure.identity
     from azure.identity import ClientSecretCredential

# --------------------------------------------------------------------------------------#
# String variables: Replace with your own
tenant = 'Your-Tenant-ID'
client = 'Your-App-Client-ID'
client_secret = 'Your-Client-Secret-Value' # See Note 2: Better to use key vault
api = 'https://analysis.windows.net/powerbi/api/.default'
# --------------------------------------------------------------------------------------#

# Generates the access token for the Service Principal
auth = ClientSecretCredential(authority = 'https://login.microsoftonline.com/',
                                                        tenant_id = tenant,
                                                        client_id = client,
                                                        client_secret = client_secret)
access_token = auth.get_token(api)
access_token = access_token.token

print('
Successfully authenticated.')   

#########################################################################################

# Note 1: This code was authored for use in a Google Colab Notebook
# Note 2: Check here for info on using Azure Key Vault: 
          # https://docs.microsoft.com/en-us/azure/key-vault/secrets/quick-create-python    
          # The above code should be modified to programmatically get the secret from AKV

#########################################################################################
# Admin: Get all links shared to the entire org.
#########################################################################################

base_url = 'https://api.powerbi.com/v1.0/myorg/'
header = {'Authorization': f'Bearer {access_token}'}

# The admin API doesn't work unless you provide a ?$top=n argument
# Top 5000 workspaces
# Admin: Get all Publish to Web artifacts

# Query for the admin scenario (everything)
# The admin API doesn't work unless you provide a ?$top=n argument
topn = '$top=5000'
admin_links = f'{base_url}admin/widelySharedArtifacts/linksSharedToWholeOrganization?{topn}'

# HTTP GET Request
whole_org_links = requests.get(admin_links, headers=header)

# Response code (200 = Success; 401 = Unauthorized; 404 = Bad Request)
print(whole_org_links)
try:
    whole_org_links = json.loads(whole_org_links.content)
except Exception as e:
    print(e)
    exit()

try:
    pd.concat([pd.json_normalize(x) for x in whole_org_links['ArtifactAccessEntities']])
except Exception:
    print('No links found.')

Code is provided as-is with no guarantees or warranties of any kind. Something I did quickly in spare time.


Get Power BI Dataset Refresh History using Python

Get Power BI Dataset Refresh History using Python

Get Workspaces from Power BI API with Python

Get Workspaces from Power BI API with Python

0