DRF:无法使用 header 中的令牌进行经过身份验证的调用
DRF: Unable to make authenticated calls with token in header
我有一个经过身份验证的列表视图,但每当我尝试使用 header 中的令牌发出请求时,我都会收到 400 错误。有什么想法吗?
列表视图
class EventListView(ListAPIView):
authentication_classes = (TokenAuthentication, )
permission_classes = (IsAuthenticated, )
serializer_class = EventFilterSerializer
...
获取身份验证令牌端点:
from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
path('auth/', obtain_auth_token, name='api_token_auth'),
]
设置:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
],
}
邮差截图
正如@Iain Shelvington 所述,尽管我在设置中配置了 DEFAULT_AUTHENTICATION_CLASSES 权限,但我在视图中覆盖了它。通过完全删除空 authentication_classes
元组或添加 TokenAuthTokenAuthenticationen,我的问题得到解决。
我有一个经过身份验证的列表视图,但每当我尝试使用 header 中的令牌发出请求时,我都会收到 400 错误。有什么想法吗?
列表视图
class EventListView(ListAPIView):
authentication_classes = (TokenAuthentication, )
permission_classes = (IsAuthenticated, )
serializer_class = EventFilterSerializer
...
获取身份验证令牌端点:
from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
path('auth/', obtain_auth_token, name='api_token_auth'),
]
设置:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
],
}
邮差截图
正如@Iain Shelvington 所述,尽管我在设置中配置了 DEFAULT_AUTHENTICATION_CLASSES 权限,但我在视图中覆盖了它。通过完全删除空 authentication_classes
元组或添加 TokenAuthTokenAuthenticationen,我的问题得到解决。