cvat-ai / cvat

Annotate better with CVAT, the industry-leading data engine for machine learning. Used and trusted by teams at any scale, for data of any scale.
https://cvat.ai
MIT License
12.34k stars 2.97k forks source link

SSO Authentication for use with CVAT Python SDK to download/export datasets via API #6736

Open KiKariJeZZa opened 1 year ago

KiKariJeZZa commented 1 year ago

My actions before raising this issue

Expected Behaviour

I should be able to authenticate using SSO and be able to use the CVAT Python SDK to write Python scripts that utilises the API to download/export dataset and whatever else I may need to do.

Current Behaviour

I am unable to find anywhere in documentation on how to do this as authentication usually requires a password:

configuration = Configuration( username='YOUR_USERNAME', password='YOUR_PASSWORD', )

whereas I only use SSO to use CVAT.

Context

By using Python Scripts to export and download datasets allows modularity for my labelling workflow architecture.

Your Environment

zhiltsov-max commented 1 year ago

Hi! I agree, it would be a nice feature, but it's not implemented yet. As a workaround, you can do the following:

  1. Open any CVAT page in browser, open the developer tools (F12 in Chrome)

  2. Open Network tab, select any request, open the Headers tab

  3. Find header values for the Authorization and Cookies headers: cvat_user_tokens

  4. Create a script like this (this one exports the tasks specified):

import os
import sys

from cvat_sdk import make_client
from cvat_sdk.core.helpers import DeferredTqdmProgressReporter

API_KEY = os.getenv("CVAT_API_KEY", "<authorization token>")
API_SESSIONID = os.getenv("CVAT_API_SESSIONID", "<session id>")
API_CSRFTOKEN = os.getenv("CVAT_API_CSRFTOKEN", "<csrf token>")

def main(argv=None):
    assert API_KEY and API_SESSIONID and API_CSRFTOKEN

    with make_client("https://app.cvat.ai", port=443) as client:
        client.api_client.set_default_header("Authorization", f"Token {API_KEY}")
        client.api_client.cookies["sessionid"] = API_SESSIONID
        client.api_client.cookies["csrftoken"] = API_CSRFTOKEN
        # client.organization_slug = ""
        client.config.status_check_period = 2

        for task_id in [1, 2, 3, 4, 5]:
            task = client.tasks.retrieve(task_id)
            task.export_dataset("CVAT for images", filename=f"task-{task_id}-cvat_for_images.zip",
                pbar=DeferredTqdmProgressReporter(), include_images=True
            )

main(sys.argv)
  1. Put the tokens into the script or define the corresponding env variables before launching the script
  2. Launch the script, e.g. python export_tasks.py or CVAT_API_KEY=... CVAT_API_SESSIONID=... CVAT_API_CSRFTOKEN=... python export_tasks.py
zhiltsov-max commented 1 year ago

Related: https://github.com/opencv/cvat/issues/4961

jerry-casuga-orica commented 1 year ago

Thank you! Seems to work great. Please update me if ever that feature gets added :)

Pari-singh commented 1 year ago

I dont see either Authorization or Cookie in my Request Headers:

zhiltsov-max commented 1 year ago

@Pari-singh, you need to login in the browser first.

pHaeusler commented 1 year ago

+1 much needed feature