EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

Sample REST API in Python: Authentication


Robert Prochowicz

Manager, Sales Engineering • MicroStrategy


Use REST APIs with Python to authenticate to MicroStrategy Intelligence Server

APIs used: POST /auth/login , GET /sessions
Use REST APIs with Python to authenticate to Strategy Intelligence Server:
With this Python script you can connect to Strategy server, authenticate, get a token, confirm if the session is still valid

  • modify variables in Paremeters section
  • if you are not using AWS Provisioning Cloud change base_url variable ("http://YouServerAddress/MicroStrategyLibrary/api/")

SAMPLE CODE


# JSON DATA PUSH API by Robert Prochowicz
# Tested with MSTR 10.10 / 2018-01-30

import requests

### Parameters ###
environmentId = '82809'
api_login = 'mstr'
api_password = 'XXXXXXXX'
base_url = "https://env-" + environmentId + ".customer.cloud.Strategy.com/StrategyLibrary/api/";


#### FUNCTIONS ###
def login(base_url,api_login,api_password):
    print("Getting token...")
    data_get = {'username': api_login,
                'password': api_password,
                'loginMode': 1}
    r = requests.post(base_url + 'auth/login', data=data_get)
    if r.ok:
        authToken = r.headers['X-MSTR-AuthToken']
        cookies = dict(r.cookies)
        print("Token: " + authToken)
        return authToken, cookies
    else:
        print("HTTP %i - %s, Message %s" % (r.status_code, r.reason, r.text))

def get_sessions(base_url, auth_token, cookies):
    print("Checking session...")
    header_gs = {'X-MSTR-AuthToken': auth_token,
                 'Accept': 'application/json'}
    r = requests.get(base_url + "sessions", headers=header_gs, cookies=cookies)
    if r.ok:
        print("Authenticated...")
        print(r)
        print("HTTP %i - %s, Message %s" % (r.status_code, r.reason, r.text))
    else:
        print("HTTP %i - %s, Message %s" % (r.status_code, r.reason, r.text))

def main():
    authToken, cookies = login(base_url,api_login,api_password)
    choice = None
    while choice != "0":
        print \
        ("""
        ---MENU---
        
        0 - Exit
        1 - Generate and Print Token
        2 - Get session
        """)

        choice = input("Your choice: ") # What To Do ???
        print()
    
        if choice == "0":
            print("Good bye!")  
        elif choice == "1":
            authToken, cookies = login(base_url,api_login,api_password)
        elif choice == "2":
            get_sessions(base_url, authToken, cookies)
        else:
            print(" ### Wrong option ### ")

### Main program    
main()

 


Comment

0 comments

Details

Example

Published:

January 30, 2018

Last Updated:

November 21, 2022