django rest 框架 TokenAuthentication 不工作
django rest framework TokenAuthentication not working
想要使用基于令牌的身份验证系统,所以 api 使用 DRF 调用获取列表,它总是抛出错误,我在本地系统中测试了这个 api。
"detail":"Authentication credentials were not provided."
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,)
url:
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 "Authorization: Bearer MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y" http://127.0.0.1:8000
想要使用基于令牌的身份验证系统,所以 api 使用 DRF 调用获取列表,它总是抛出错误,我在本地系统中测试了这个 api。
"detail":"Authentication credentials were not provided."
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,)
url:
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 "Authorization: Bearer MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y" http://127.0.0.1:8000