[英]Django REST Framework TokenAuthentication to authenticate in Django generally
[英]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.