Forum Discussion
Subscribing Power BI reports through API
Hi Anonymous,
When you say, it doesn't work as expected, what do you mean? Are you getting an error code?
If so, can you share a screenshot of your current flow setup that is not working (or explain it in more detail)?
Hi Expiscornovus
This is code i used for subcribing reports through API
--Code--
import requests
import json
# Define constants
CLIENT_ID = '#######################'
CLIENT_SECRET = '#######################'
TENANT_ID = '#######################'
RESOURCE = 'https://analysis.windows.net/powerbi/api'
API_URL = 'https://api.powerbi.com/v1.0/myorg/groups/{GROUP_ID}/reports/{REPORT_ID}/subscriptions'
def get_access_token():
url = f'https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
body = {
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'scope': RESOURCE + '/.default'
}
response = requests.post(url, headers=headers, data=body)
if response.status_code == 200:
return response.json().get('access_token')
else:
print(f"Failed to obtain access token. Status Code: {response.status_code}, Response: {response.text}")
return None
def create_subscription(access_token, group_id, report_id):
url = API_URL.format(GROUP_ID=group_id, REPORT_ID=report_id)
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
data = {
"displayName": "My Subscription",
"schedule": {
"frequency": "Daily",
"timeZone": "Pacific Standard Time",
"times": ["07:00"]
},
"status": "Enabled"
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 201:
print("Subscription created successfully.")
else:
print(f"Failed to create subscription. Status Code: {response.status_code}, Response: {response.text}")
if __name__ == "__main__":
# Replace with your workspace and report IDs
GROUP_ID = '########################'
REPORT_ID = '#######################'
token = get_access_token()
if token:
create_subscription(token, GROUP_ID, REPORT_ID)
Error
Failed to create subscription. Status Code: 404, Response: {
"error":{
"code":"","message":"No HTTP resource was found that matches the request URI}}