簡體   English   中英

Django Rest框架TokenAuthentication不起作用

[英]django rest framework TokenAuthentication not working

想要使用基於令牌的身份驗證系統,因此api使用DRF調用獲取列表,它總是拋出錯誤,我在本地系統中測試了此api。

“ detail”:“未提供身份驗證憑據。”

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

class MyListSerializer(SignUpSerializer):

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

view.py

class MyList(generics.ListCreateAPIView):

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

網址:

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

錯誤:

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

問題:

class MyList(generics.ListCreateAPIView):

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

解:

class MyList(generics.ListCreateAPIView):

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

curl -H“授權:承載MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y” http://127.0.0.1:8000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM