Reputation: 23
``I am trying to implement token authentication in my Django Rest Framework application. I am able to receive a token from the django server but when I send a request with 'Authorization' header, I receive the following error:
{ "detail": "Authentication credentials were not provided." }
I have also checked the request.META and there is no key with name "HTTP_AUTHORIZATION". I am running my django app on development server.
Views.py
# Create your views here.
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.decorators import api_view,authentication_classes,permission_classes
from django.contrib.auth.decorators import permission_required,login_required
import google.cloud.storage
import uuid
import psycopg2
@api_view(['GET'])
@authentication_classes((SessionAuthentication, BasicAuthentication))
@permission_classes((IsAuthenticated,))
def auth_test(request):
return Response("it's great to have you here with token auth!!")
@api_view(['GET'])
def free_test(request):
#print(request.META['HTTP_AUTHORIZATION'])
return Response("entry without token auth !!")
What is the reason that my authorization credentials are not received by django server?
POSTMAN SCREENSHOT WHERE I HAVE PASSED THE AUTHORIZATION HEADER
Upvotes: 1
Views: 10436
Reputation: 88629
You are trying to access your view using Token
auth method, and you are not mentioned that in authentication_classes
. Try the following code
from rest_framework.authentication import TokenAuthentication
@api_view(['GET'])
@authentication_classes((SessionAuthentication, TokenAuthentication, BasicAuthentication))
@permission_classes((IsAuthenticated,))
def auth_test(request):
return Response("it's great to have you here with token auth!!")
Upvotes: 3