# Embedded python power BI

**URL:** <https://pyar.discourse.group/t/embedded-python-power-bi/1132>\
**Category:** Sin categoría\
**Created:** [23 Mayo, 2023 07:53 UTC](https://pyar.discourse.group/t/embedded-python-power-bi/1132 "2023-05-23T07:53:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![peya02](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@peya02](https://pyar.discourse.group/u/peya02)\
**Post date:** [23 Mayo, 2023 07:53 UTC](https://pyar.discourse.group/t/embedded-python-power-bi/1132/1 "2023-05-23T07:53:55Z")

</div>

Tengo una app que hace embedded de power bi Por un lado tengo def get\_embed\_info() que llama a PbiEmbedService que genera el embedded con los datos de worskpace, report\_id…

```auto
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

```

A su vez, esta classe llamada a get\_request\_header() que a su vez necesita get\_access\_token()

```auto
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))

```

Bien, por ultimo tengo la def all\_workshop que lo que hace es generar lista de todas las aéreas de trabajo de un usuario

```auto
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'])

```

Este código necesita dos parámetros (app y user) los cuales deberían venir de la def get\_request\_header() que su vez deberían venir de get\_access\_token(). Entiendo que el parametro app es la propia aplicacion, y user deberia cogerlo de POWER\_BI\_USER El problema es que no se como enviar estos parámetros des de get\_access\_token() ¿Pueden ayudarme? Gracias.
