MicroStrategy ONE

Se connecter à MicroStrategy à l'aide d'un jeton d'identité Workstation

Le mstrio-py La Library Python fournit un Module de connexion pour démarrer une session avec Intelligence Server. Il prend en charge les modes d'authentification Standard et LDAP.

Pour lancer la session, vous devez insérer l'URL du serveur de l'API REST MicroStrategy, l'ID du projet, le nom d'utilisateur et le mot de passe.

Copier
from mstrio.connection import Connection

base_url = "https://your-microstrategy-server.com/MicroStrategyLibrary/api"
mstr_username = "Username"
mstr_password = "Password"
project_id = "PROJECT_ID"
conn = Connection(base_url, mstr_username, mstr_password, project_id=project_id)

Les scripts exécutés dans Workstation peuvent être modifiés pour accéder à la variable globale workstationData et utiliser le jeton d'identité de l'environnement dans lequel il est actuellement enregistré. En lançant la session de cette façon, les utilisateurs n'ont plus à insérer l'URL du serveur de l'API REST MicroStrategy et leurs informations d'identification dans le script.

Copier
import requests
from typing import Union
from mstrio.connection import Connection
from requests.cookies import RequestsCookieJar

def get_connection(workstation_data: dict, project_id: str = None,
                   project_name: str = None) -> Union[Connection, None]:
    """Get connection based on data saved in memory of Workstation. It is
    possible to provide 'project_id' or 'project_name' to select project.
    
    When both `project_id` and `project_name` are `None`, project selection
    is cleared. When both `project_id` and `project_name` are provided,
    `project_name` is ignored.
    
    Project can be also selected later by calling function 'select_project'
    on Connection object.
    
    Args:
        workstation_data (object): object which is stored in a 'workstationData'
            variable within Workstation
        project_id (str, optional): id of project to select
        project_name (str, optional): name of project to select
        
    Returns:
        connection to I-Server or None is case of some error
    """
    
    try:
        print('Creating connection from Workstation Data object...', flush=True)
        # get base url from Workstation Data object
        base_url = workstation_data['defaultEnvironment']['url']
        # get headers from Workstation Data object
        headers = workstation_data['defaultEnvironment']['headers']
        # get cookies from Workstation Data object
        cookies = workstation_data['defaultEnvironment']['cookies']
        # prepare cookies for request
        jar = RequestsCookieJar()
        for c in cookies:
            cookie_values = {'domain': '', 'path': '/'}
            cookie_values.update(c)
            jar.set(cookie_values['name'], cookie_values['value'],
                    domain=cookie_values['domain'], path=cookie_values['path'])
    except Exception as e:
        print('Some error occurred while preparing data to get identity token:')
        print(e)
        return None
    # get identity token
    r = requests.post(base_url + 'api/auth/identityToken', headers=headers, cookies=jar)
    if r.ok:
        # create connection to I-Server
        return Connection(base_url, identity_token=r.headers['X-MSTR-IdentityToken'],
                          project_id=project_id, project_name=project_name)
    else:
        print('HTTP %i - %s, Message %s' % (r.status_code, r.reason, r.text), flush=True)
        return None
    
# get connection without selecting project
# conn = get_connection(workstationData)
# select project for connection
# conn.select_project(project_id = mstr_tutorial_id, project_name)
# get connection and select project (you can use id or name of project)

mstr_tutorial_id = 'B7CA92F04B9FAE8D941C3E9B7E0CD754'
mstr_tutorial_name = 'MicroStrategy Tutorial'

conn = get_connection(workstation_data=workstationData, project_id=mstr_tutorial_id)