简体   繁体   English

Django-rest-framework {“详细信息”:“未提供身份验证凭据。” } 使用 django-rest-knox

[英]Django-rest-framework {“detail”: “Authentication credentials were not provided.” } using django-rest-knox

I see the error {"detail": "Authentication credentials were not provided." }我看到错误{"detail": "Authentication credentials were not provided." } {"detail": "Authentication credentials were not provided." } . {"detail": "Authentication credentials were not provided." }

This is the code I am using for Login这是我用于登录的代码

My Model:我的 Model:

class User(AbstractBaseUser):
    STUDENT = 'STU'
    SCHOOL = 'SCH'
    INSTITUTE = 'INST'
    TUTOR = 'TUT'
    ACCOUNT_TYPE_CHOICES = [
        (STUDENT, 'Student'),
        (SCHOOL, 'School'),
        (INSTITUTE, 'Institute'),
        (TUTOR, 'Tutor'),
    ]
    account_type = models.CharField(
        max_length=4,
        choices=ACCOUNT_TYPE_CHOICES,
        default=SCHOOL,
    )
    name = models.CharField(max_length=255)
    email = models.EmailField(unique=True,max_length=255)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_admin = models.BooleanField(default=False)
    timestamp = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name','account_type']

    objects=UserManager()

    def __str__(self):
        return self.email

    def has_perm(self,perm,obj=None):
        return True

    def has_module_perms(self,app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

My serializers:我的序列化器:

from rest_framework import serializers
from .models import User
from django.contrib.auth import authenticate

# User Serializer


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'account_type', 'name', 'email')

# Login Serializer


# I guess the validate function is not working .

class LoginSerializer(serializers.Serializer):
    email = serializers.EmailField()
    password = serializers.CharField()

    def validate(self, data):
        user = authenticate(request=None,**data)
        if user and user.is_active:
            return user
        raise serializers.ValidationError("Incorrect Credentials")

My Views:我的观点:

from rest_framework import generics, permissions,authentication
from rest_framework.response import Response
from knox.models import AuthToken
from knox.views import LoginView as KnoxLoginView
from .serializers import UserSerializer, RegisterSerializer, LoginSerializer
from django.contrib.auth import authenticate,login

# Login API


class LoginAPI(generics.GenericAPIView):
    serializer_class = LoginSerializer
    # authentication_class=[authentication.BasicAuthentication]
    def post(self, request, *args, **kwargs):
        serializer = LoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)[1]
        })

My Urls:我的网址:

from django.urls import path, include
from .api import RegisterAPI, LoginAPI
from knox import views as knox_views

urlpatterns = [
    path('api/auth/', include('knox.urls')),
    path('api/auth/register/', RegisterAPI.as_view()),
    path('api/auth/login/', LoginAPI.as_view()),
    path('api/auth/logout/', knox_views.LogoutView.as_view(), name='knox_logout')
]

Also when I provide the token generated while registration, it gives user is inactive or dead.But when i check my database and the token expiry time its still active.此外,当我提供注册时生成的令牌时,它会使用户处于非活动状态或死亡状态。但是当我检查我的数据库和令牌到期时间时,它仍然处于活动状态。 I have tried different third party libraries like rest-auth it also gives the same error.I have checked many other answers regarding the same topic too but applying them also isn't helping.我尝试了不同的第三方库,例如 rest-auth,它也给出了相同的错误。我也检查了有关同一主题的许多其他答案,但应用它们也无济于事。

You should confirm from your settings.py file in the REST_FRAMEWORK settings that you have the 'DEFAULT_PERMISSION_CLASSES' set to 'rest_framework.permissions.AllowAny'.您应该从 REST_FRAMEWORK 设置中的 settings.py 文件确认您已将“DEFAULT_PERMISSION_CLASSES”设置为“rest_framework.permissions.AllowAny”。

it should be like:它应该是这样的:

# Rest framework
REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny',
    ],
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django Rest Framework {“detail”:“未提供身份验证凭据。”} - Django Rest Framework {“detail”:“Authentication credentials were not provided.”} 自定义Django休息框架身份验证响应{“详细信息”:“未提供身份验证凭据。”} - Customize Django rest framework authentication response {“detail”: “Authentication credentials were not provided.”} Django Rest Framework JWT“未提供身份验证凭据。”} - Django Rest Framework JWT “Authentication credentials were not provided.”} django rest API开发使用Swagger UI,有“详细信息”:“未提供身份验证凭据。” - django rest API development Using Swagger UI, got“detail”: “Authentication credentials were not provided.” Django:“详细信息”:“未提供身份验证凭据。” - Django : “detail”: “Authentication credentials were not provided.” Python 请求与 Django Rest 框架 - “详细信息”:“未提供身份验证凭据” - Python Requests with Django Rest Framework - 'detail': 'Authentication credentials were not provided' Django Rest 框架 - 未提供身份验证凭据 - Django Rest Framework - Authentication credentials were not provided django rest 框架中未提供错误身份验证凭据 - getting error Authentication credentials were not provided in django rest framework DRF:“详细信息”:“未提供身份验证凭据。” - DRF: “detail”: “Authentication credentials were not provided.” 禁止:/api/v1.0/user/create-user/ & {“detail”:“未提供身份验证凭据。” } DJANGO - Forbidden: /api/v1.0/user/create-user/ & { "detail": "Authentication credentials were not provided." } DJANGO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM