Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication credentials were not provided drf

when i trying to access api i'm getting this error:

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

i have included this in settings.py:

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':(
    'rest_framework.authentication.TokenAuthentication',
    'rest_framework.authentication.SessionAuthentication',


),
'DEFAULT_PERMISSION_CLASSES':(
    'rest_framework.permissions.IsAuthenticated',
)

}

my app api urls.py:

from django.urls import path,include
from . import views
from rest_framework import routers

router = routers.SimpleRouter()
router.register(r'',views.UserViewSet, 'user_list')
urlpatterns = router.urls

my views.py:

class UserViewSet(viewsets.ModelViewSet):
       queryset = User.object.all()
       serializer_class = serializers.UserSerializers

serializers.py:

from rest_framework import serializers
from users.models import User


class UserSerializers(serializers.ModelSerializer):
  class Meta:
    model = User
    fields = ('email','password')

my main urls.py:

urlpatterns = [
path('admin/', admin.site.urls),
path('',include(urls)),
path ('', include(user_urls)),
path('api/',include(api_urls)),

when i running localhost:8000/api i'm getting the error

like image 411
afk Avatar asked Apr 07 '26 00:04

afk


1 Answers

You can't access the api from the browsers url if you are using TokenAuthentication. as said by @DarkOrb TokenAuthentication expects a authorization header with token as it's value. So You must pass token whenever you call the api. You can test your api using postman.

enter image description here

In above image i have passed token in headers of postman to access my api. When you call your api from frontend side,pass your token along with the request. If you just want to use your api in only desktop's browser,in that case you can use SessionAuthentication only.For mobile devices Tokenauthentication must be done.

like image 149
Gilbish Kosma Avatar answered Apr 08 '26 15:04

Gilbish Kosma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!