简体   繁体   English

Django Rest框架TokenAuthentication不起作用

[英]django rest framework TokenAuthentication not working

Want to use token base authentication system so api call for get list using DRF , It always throw error , I test this api in local system. 想要使用基于令牌的身份验证系统,因此api使用DRF调用获取列表,它总是抛出错误,我在本地系统中测试了此api。

"detail":"Authentication credentials were not provided." “ detail”:“未提供身份验证凭据。”

Setting.py Setting.py

REST_FRAMEWORK = {

    'DEFAULT_AUTHENTICATION_CLASSES': (
        #'rest_framework.permissions.IsAuthenticated',

        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',        
        'rest_framework.authentication.TokenAuthentication',        
    ),

    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),

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

    ),       
}

Serializer.py Serializer.py

class MyListSerializer(SignUpSerializer):

    class Meta:
        model = MyMod
        fields = ('no', 'yes')       

view.py view.py

class MyList(generics.ListCreateAPIView):

    queryset = MyMod.objects.all()
    serializer_class = MyListSerializer
    authentication_classes = (TokenAuthentication,)

url: 网址:

curl -H "Authorization: Bearer MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y" http://127.0.0.1:8000/my/

Error: 错误:

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

Problem : 问题:

class MyList(generics.ListCreateAPIView):

    queryset = MyMod.objects.all()
    serializer_class = MyListSerializer
    authentication_classes = (TokenAuthentication,)

Solution: 解:

class MyList(generics.ListCreateAPIView):

    queryset = MyMod.objects.all()
    serializer_class = MyListSerializer
    permission_classes = [TokenHasReadWriteScope]

curl -H "Authorization: Bearer MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y" http://127.0.0.1:8000 curl -H“授权:承载MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y” http://127.0.0.1:8000

暂无
暂无

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

相关问题 Django REST Framework TokenAuthentication 一般在 Django 中进行身份验证 - Django REST Framework TokenAuthentication to authenticate in Django generally Django REST框架TokenAuthentication返回匿名用户 - Django REST framework TokenAuthentication returns anonymous user 使用 Django REST Framework 的 TokenAuthentication 查询字符串中的令牌 - Token in query string with Django REST Framework's TokenAuthentication "detail": "方法 \\"GET\\" 不允许。" 在 TokenAuthentication Django 休息框架中 - "detail": "Method \"GET\" not allowed." in TokenAuthentication Django rest framework 如何在django-rest-framework中使用TokenAuthentication for API - How to use TokenAuthentication for API in django-rest-framework 在 django rest 框架(使用 TokenAuthentication)公共 APIView 上获取 CSRF 令牌丢失错误 - Getting CSRF token missing error on a django rest framework (with TokenAuthentication) public APIView 根据请求的类型,在API视图的一部分上应用tokenauthentication Django rest框架 - Apply tokenauthentication Django rest framework on part of the API view based on the type of the request 在 Django Rest 框架中使用 Tokenauthentication 进行身份验证时,last_login 字段未更新 - last_login field is not updated when authenticating using Tokenauthentication in Django Rest Framework Django REST Framework URLPathVersioning不起作用 - Django REST Framework URLPathVersioning not working Django Rest 框架:路由器不工作 - Django Rest Framework: router is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM