Search code examples
apache-superset

Problem importing a dashboard: The response shows a login page


When I use the Superset's API to import a dashboard the response shows me a login page.

I am doing the request using Python.

import requests

headers = {
    'accept': 'application/json', 
    'Authorization': f'Bearer {jwt_token}', 
    'X-CSRFToken': csrf_token,
    'Referer': url
}
files = { 
    'formData': (
        dashboard_path, 
        open(dashboard_path, 'rb'), 
        'application/json'
    )
}

response = requests.get(url, files=files, headers=headers)

Does anyone know how to solve this problem?


Solution

  • I had some trouble with the Superset API myself, mostly because I did not handle the CSRF Token correctly:

    It seems to be important that the retrieval of the JWT Token, the CSRF Token and the actual request happen in the same session.

    If I don't do that, I can reproduce your error and are also sent to the login page (also you use a GET request in this example, but it should be POST).

    Here an example from my local test-setup:

    
    import requests
    
    session = requests.session()
    
    jwt_token = session.post(
        url='http://localhost:8088/api/v1/security/login',
        json={
        "username": "admin",
        "password": "admin",
        "refresh": False,
        "provider": "db"
        }
    ).json()["access_token"]
    
    csrf_token = session.get(
        url='http://localhost:8088/api/v1/security/csrf_token/',
        headers={
            'Authorization': f'Bearer {jwt_token}',
        }
    ).json()["result"]
    
    headers = {
        'accept': 'application/json',
        'Authorization': f'Bearer {jwt_token}',
        'X-CSRFToken': csrf_token,
    }
    
    response = requests.post(
        'http://localhost:8088/api/v1/dashboard/import',
        headers=headers,
        files={
            "formData": ('dashboards.json', open("dashboards.json", "rb"), 'application/json')
        },
    )
    
    session.close()