I'm working on a store site, where every user is going to be anonymous (well, until it's time to pay at least), and I'm trying to use Django REST Framework to serve the product API, but it keeps complaining about:
"detail": "Authentication credentials were not provided."
I found some settings related to authentication, but I couldn't find anything like ENABLE_AUTHENTICATION = True
. How do I simply disable authentication, and let any visitor to the site access the API?
Basic Authentication in Django REST Framework uses HTTP Basic Authentication. It is generally appropriate for testing. The REST framework will attempt to authenticate the Basic Authentication class and set the returned values to request. user and request.
To use session authentication, you must create a session first. You must have a login resource, which accepts user credentials and authenticates a user, using the Django authentication system. On requesting that resource the client will get a cookie header. The cookie and csrf token must be used in future requests.
Token authentication refers to exchanging username and password for a token that will be used in all subsequent requests so to identify the user on the server side. This article revolves about implementing token authentication using Django REST Framework to make an API.
You can give empty defaults for the permission and authentication classes in your settings.
REST_FRAMEWORK = { # other settings... 'DEFAULT_AUTHENTICATION_CLASSES': [], 'DEFAULT_PERMISSION_CLASSES': [], }
You can also disable authentication for particular class or method, just keep blank the decorators for the particular method.
from rest_framework.decorators import authentication_classes, permission_classes @api_view(['POST']) @authentication_classes([]) @permission_classes([]) def items(request): return Response({"message":"Hello world!"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With