Files
chat/update_token.py
Andrew K. Choi 76d0d86211
All checks were successful
continuous-integration/drone/push Build is passing
calendar events
2025-09-26 15:57:50 +09:00

83 lines
3.0 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
import requests
import json
# Базовый URL для тестирования
BASE_URL = "http://localhost:8001" # User service
def register_user():
"""Регистрация нового пользователя"""
url = f"{BASE_URL}/api/v1/auth/register"
data = {
"email": "test_mobile@example.com",
"password": "password123",
"first_name": "Test",
"last_name": "Mobile"
}
print(f"Регистрация пользователя: {json.dumps(data, ensure_ascii=False)}")
try:
response = requests.post(url, json=data)
if response.status_code == 201:
print("✅ Пользователь успешно зарегистрирован")
user_data = response.json()
print(f"ID пользователя: {user_data.get('id')}")
return user_data
else:
print(f"❌ Ошибка при регистрации: {response.status_code}")
print(f"Ответ: {response.text}")
return None
except Exception as e:
print(f"❌ Исключение: {str(e)}")
return None
def login_user(email, password):
"""Вход пользователя и получение токена"""
url = f"{BASE_URL}/api/v1/auth/token"
data = {
"username": email,
"password": password
}
print(f"Вход пользователя: {email}")
try:
response = requests.post(url, data=data)
if response.status_code == 200:
token_data = response.json()
print("✅ Успешный вход")
# Сохраняем токен
token = token_data.get("access_token")
with open("auth_token.txt", "w") as f:
f.write(token)
print(f"✅ Токен сохранен в файл auth_token.txt")
return token
else:
print(f"❌ Ошибка при входе: {response.status_code}")
print(f"Ответ: {response.text}")
return None
except Exception as e:
print(f"❌ Исключение: {str(e)}")
return None
if __name__ == "__main__":
print("=== Обновление токена авторизации ===")
# Пробуем выполнить вход с существующими данными
token = login_user("test_mobile@example.com", "password123")
if not token:
# Если не удалось войти, регистрируем нового пользователя
user = register_user()
if user:
token = login_user("test_mobile@example.com", "password123")
print("\nПроцесс обновления токена завершен!")
if token:
print("Теперь вы можете использовать скрипт test_mobile_api.py для тестирования API")