Django Rest Framework Token Authentication Not Working in production

I have deployed the Django Application on VPS server with following configuration.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "rest_framework",
    "rest_framework.authtoken",
    "stocks",
    "stock_api",
]

# REST Framework Configurations
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework.authentication.TokenAuthentication",
    ],
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
}

I have tested the following code on my local machine and it works well. But in production, it shows an error as Credential not provided.

import requests

headers = {"Authorization": "Token 0f2c3e612ffba7d7b45164b8c8567dc56c113347"}

response = requests.get(endpoint, headers=headers)
print(response.status_code)
print(response.json())

Error

status_code : 401

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

I have tested the same Code on my local machine, but it is showing ok status.

Back to Top