King
King

Reputation: 2025

Authentication credentials were not provided django-rest-auth

I have an application which I implemented django restframework and django reat-auth and jango framework jwt. I followed the instructions and every thing works fine in the browser. I now decided to test out the connection in postman and trying to get the logged in user's details which the endpoint is /rest-auth/user but i get the following error

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

and I decided to copy the returned Token and put it in the header of the user url as

"Authorization": "Token ahagjbeghq7hbcvgqhvwqu08hevug.jwhhwiiwhw",
"Content-Type": "application/json; charset=utf-8"

after modifying the header with the returned token I expected it to display the user's details but instead I still got

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

but I can verify that the token is correct through the url provided by the restframework jwt

/api-token-verify which retuns the token value back. this is my django rest authentication classes

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',

    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
        'rest_framework.permissions.IsAuthenticatedOrReadOnly',

    ),

}

Upvotes: 3

Views: 9387

Answers (2)

Sabuhi Shukurov
Sabuhi Shukurov

Reputation: 1920

As @neverwalkaloner mentioned already JWT keyword suppose to be the value of Authorization in the header section of Postman. Docs

Additionally, if you don't want JWT as a keyword in with your token, you can customize it from your settings: with following key: JWT_AUTH_HEADER_PREFIX

JWT_AUTH = {
    'JWT_ENCODE_HANDLER':
    'rest_framework_jwt.utils.jwt_encode_handler',

    'JWT_DECODE_HANDLER':
    'rest_framework_jwt.utils.jwt_decode_handler',

    'JWT_PAYLOAD_HANDLER':
    'rest_framework_jwt.utils.jwt_payload_handler',

    'JWT_PAYLOAD_GET_USER_ID_HANDLER':
    'rest_framework_jwt.utils.jwt_get_user_id_from_payload_handler',

    'JWT_RESPONSE_PAYLOAD_HANDLER':
    'rest_framework_jwt.utils.jwt_response_payload_handler',

    'JWT_SECRET_KEY': settings.SECRET_KEY,
    'JWT_GET_USER_SECRET_KEY': None,
    'JWT_PUBLIC_KEY': None,
    'JWT_PRIVATE_KEY': None,
    'JWT_ALGORITHM': 'HS256',
    'JWT_VERIFY': True,
    'JWT_VERIFY_EXPIRATION': True,
    'JWT_LEEWAY': 0,
    'JWT_EXPIRATION_DELTA': datetime.timedelta(days=1),
    'JWT_AUDIENCE': None,
    'JWT_ISSUER': None,

    'JWT_ALLOW_REFRESH': True,
    'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),  
    'JWT_AUTH_HEADER_PREFIX': 'Bearer', #this most commonly accepted way
    'JWT_AUTH_COOKIE': None,

}

Upvotes: 0

neverwalkaloner
neverwalkaloner

Reputation: 47354

You should use JWT instead of Token inside Authorization header for jwt token:

"Authorization: JWT <your_token>"

Upvotes: 6

Related Questions