Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Disable Authentication in Django REST Framework

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?

like image 606
machineghost Avatar asked Nov 23 '14 03:11

machineghost


People also ask

What is basic authentication in Django REST framework?

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.

How does Django implement session authentication?

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.

What is token authentication in Django?

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.


2 Answers

You can give empty defaults for the permission and authentication classes in your settings.

REST_FRAMEWORK = {     # other settings...      'DEFAULT_AUTHENTICATION_CLASSES': [],     'DEFAULT_PERMISSION_CLASSES': [], } 
like image 63
inancsevinc Avatar answered Sep 19 '22 17:09

inancsevinc


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!"}) 
like image 25
Aashish Soni Avatar answered Sep 19 '22 17:09

Aashish Soni