Files
PowerBI-Embeded/powerbi_embed4.py
T
2025-08-03 21:19:24 +00:00

64 lines
2.5 KiB
Python

import requests
import adal
def get_access_token(client_id, client_secret):
authority_url = 'https://login.microsoftonline.com/mathworks.onmicrosoft.com'
context = adal.AuthenticationContext(
authority_url,
validate_authority=True,
api_version=None
)
token = context.acquire_token_with_username_password(
# token = context.acquire_token_with_client_credentials(
resource='https://analysis.windows.net/powerbi/api',
username= username,
password= password,
# client_secret= secret_id,
client_id= client_id,
# scope= 'openid',
# tenant= 'mathworks.onmicrosoft.com',
)
access_token = token['accessToken']
return (access_token)
def get_embed_token(group_id, report_id, access_token):
dest = 'https://api.powerbi.com/v1.0/myorg/groups/' + group_id + '/reports/' + report_id + '/GenerateToken'
embed_url = 'https://app.powerbi.com/reportEmbed?reportId=' + report_id + '&groupId=' + group_id
headers = {'Authorization': 'Bearer ' + access_token}
settings = {'accessLevel': 'View',
'allowSaveAs': 'false'}
#vals.Add(new KeyValuePair<string, string>("grant_type", "password"));
#vals.Add(new KeyValuePair<string, string>("scope", "openid"));
#vals.Add(new KeyValuePair<string, string>("resource", "https://analysis.windows.net/powerbi/api"));
#vals.Add(new KeyValuePair<string, string>("client_id", ""));
#vals.Add(new KeyValuePair<string, string>("client_secret", ""));
#vals.Add(new KeyValuePair<string, string>("username", ""));
#vals.Add(new KeyValuePair<string, string>("password", ""));
response = requests.post(dest, data=settings, headers=headers)
print('Embed token response: ', response.status_code, response.reason, response.text)
token = response.json().get('token')
return {'token': token, 'embed_url': embed_url, 'report_id': report_id}
if __name__ == "__main__":
client_id = '**********'
secret_id = '********'
group_id = '********'
report_id = '*********'
password = '******'
username = '********'
# First we get the access token for our app (?)
access_token = get_access_token(client_id, secret_id)
print('access token: {}'.format(access_token))
# Then we get an embed token so we can "embed" on a web page (?)
embed_token = get_embed_token(group_id, report_id, access_token)
print('embed token response', embed_token)
# Then we can use javascript on our web page to embed the controls/data
# (TBD)