1. Configurar Token Authentication en DRF. Agregar en settings.py
INSTALLED_APPS = [
...
'rest_framework',
'rest_framework.authtoken',
]
  1. Aplicar las migraciones necesarias
python [manage.py](<http://manage.py/>) makemigrations
python [manage.py](<http://manage.py/>) migrate
  1. Configurar la autenticación por defecto

    En settings.py, se define el método de autenticación predeterminado:

    REST_FRAMEWORK = {
    	'DEFAULT_AUTHENTICATION_CLASSES': [
    	'rest_framework.authentication.TokenAuthentication',
    	],
    	'DEFAULT_PERMISSION_CLASSES': [
    	'rest_framework.permissions.IsAuthenticated',
    	],
    }
    
  2. Genera un token para este usuario. Primero deber crear un super usuario.

    Crear super Usuario: python manage.py createsuperuser

    Crea el token: python manage.py drf_create_token <username>

  3. Configurar una vista de inicio de sesión (endpoint para obtener el token) en urls.py (del proyecto general)

    from rest_framework.authtoken.views import obtain_auth_token
    from django.urls import path
    
    urlpatterns = [
    path('api-token-auth/', obtain_auth_token, name='api_token_auth'),
    ]
    
  4. Autenticarse en la API. Obtener token

• URL: /api-token-auth/ • Método: POST • Body

{
"username": "your_username",
"password": "your_password"
}

• Response

{
"token": "9a5f1b2d3e4f6789abcdef0123456789"
}
  1. Agregar token en headers. Para usar el token, agrega el encabezado Authorization en cada solicitud.

    Authorization: {token}