I have an app that embeds power bi. On the one hand, I have def get_embed_info() that calls PbiEmbedService that generates the embedded with the data from worskpace, report_id…
def get_embed_info():
'''Returns report embed configuration'''
config_result = Utils.check_config(app)
if config_result is not None:
return json.dumps({'errorMsg': config_result}), 500
try:
embed_info = PbiEmbedService().get_embed_params_for_single_report(app.config['WORKSPACE_ID'], app.config['REPORT_ID'])
return embed_info
except Exception as ex:
return json.dumps({'errorMsg': str(ex)}), 500
In turn, this class calls get_request_header() which in turn needs get_access_token()
def get_request_header(self):
return {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + AadService.get_access_token()}
class AadService:
def get_access_token():
'''Generates and returns Access token
Returns:
string: Access token
'''
response = None
try:
if app.config['AUTHENTICATION_MODE'].lower() == 'masteruser':
# Create a public client to authorize the app with the AAD app
clientapp = msal.PublicClientApplication(app.config['CLIENT_ID'], authority=app.config['AUTHORITY_URL'])
accounts = clientapp.get_accounts(username=app.config['POWER_BI_USER'])
if accounts:
# Retrieve Access token from user cache if available
response = clientapp.acquire_token_silent(app.config['SCOPE_BASE'], account=accounts[0])
if not response:
# Make a client call if Access token is not available in cache
response = clientapp.acquire_token_by_username_password(app.config['POWER_BI_USER'], app.config['POWER_BI_PASS'], scopes=app.config['SCOPE_BASE'])
try:
return response['access_token']
except KeyError:
raise Exception(response['error_description'])
except Exception as ex:
raise Exception('Error retrieving Access token\n' + str(ex))
Well, finally I have the def all_workshop that what it does is generate a list of all the work areas of a user
def all_workshop(app,user):
url='https://api.powerbi.com/v1.0/myorg/groups'
api_response = requests.get(url, headers=get_request_header(app,session))
if api_response.status_code != 200:
abort(api_response.status_code, description=f'Error while retrieving Embed URL\n{api_response.reason}:\t{api_response.text}\nRequestId:\t{api_response.headers.get("RequestId")}')
api_response = json.loads(api_response.text)
api_response['value']
return (api_response['value'])
This code needs two parameters (app and user) which should come from the get_request_header() def which should in turn come from get_access_token(). I understand that the app parameter is the application itself, and user should take it from POWER_BI_USER The problem is that I don’t know how to send these parameters from get_access_token() Can you help me?
Thank you.