Перейти к основному содержимому

Аутентификация

DocAI использует JWT (JSON Web Tokens) для аутентификации API запросов.

Получение токена

Endpoint

POST /api/token/

Запрос

curl -X POST http://localhost:8000/api/token/ \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "your-password"
}'

Ответ

{
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
}

Использование токена

Добавьте токен в заголовок Authorization:

curl -X POST http://localhost:8000/ai_agents/api/v1/classify/ \
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." \
-H "Content-Type: application/json" \
-d '{"ocr_draft": "Document text..."}'

Обновление токена

Когда access токен истечёт, используйте refresh токен для получения нового.

Endpoint

POST /api/token/refresh/

Запрос

curl -X POST http://localhost:8000/api/token/refresh/ \
-H "Content-Type: application/json" \
-d '{
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
}'

Ответ

{
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
}

Время жизни токенов

ТокенВремя жизни
Access30 дней
Refresh60 дней

Публичные endpoints

Некоторые endpoints не требуют аутентификации:

EndpointОписание
POST /documents/api/v1/request/Загрузка документов
GET /documents/api/v1/request/{id}/Получение результатов

Защищённые endpoints

Требуют JWT токен:

EndpointОписание
POST /ai_agents/api/v1/classify/Классификация
POST /ai_agents/api/v1/extract/Извлечение данных

Примеры на разных языках

Python

import requests

# Получение токена
response = requests.post(
"http://localhost:8000/api/token/",
json={"email": "user@example.com", "password": "password"}
)
token = response.json()["access"]

# Использование токена
headers = {"Authorization": f"Bearer {token}"}
response = requests.post(
"http://localhost:8000/ai_agents/api/v1/classify/",
headers=headers,
json={"ocr_draft": "Document text..."}
)

JavaScript

// Получение токена
const tokenResponse = await fetch("http://localhost:8000/api/token/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "user@example.com",
password: "password"
})
});
const { access } = await tokenResponse.json();

// Использование токена
const response = await fetch("http://localhost:8000/ai_agents/api/v1/classify/", {
method: "POST",
headers: {
"Authorization": `Bearer ${access}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ ocr_draft: "Document text..." })
});

Ошибки аутентификации

401 Unauthorized

{
"detail": "Given token not valid for any token type",
"code": "token_not_valid"
}

Решение: Обновите токен или получите новый.

403 Forbidden

{
"detail": "Authentication credentials were not provided."
}

Решение: Добавьте заголовок Authorization.