[英]DRF Token Authentication: { “detail”: “Authentication credentials were not provided.” }
I am working on a Django e-commerce project. 我正在从事Django电子商务项目。
I tried to implement a token authentication system. 我试图实现令牌认证系统。 I added rest_framework.authtoken
to INSTALLED_APPS
and 我添加了rest_framework.authtoken
到INSTALLED_APPS
和
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
to settings file. 到设置文件。
Here's my urls.py
这是我的urls.py
path('admin/', admin.site.urls),
path('login_signup/',include('login_signup.urls')),
path('profile',include('profile_info.urls')),
path('',include('category.urls')),
path('cart',include('cart.urls')),
path('api_token_auth',views.obtain_auth_token,name='auth-token'),
]
Here's my category.urls
file. 这是我的category.urls
文件。
urlpatterns=[
path('category',CategoryView.as_view(),name='category'),
path('subcategory',SubcategoryView.as_view(),name='subcategory'),
path('product',ProductView.as_view(),name='product'),
path('search',SearchView.as_view(),name='search'),
]
I am able to obtain tokens by providing username and password at api_token_auth
end point. 我可以通过在api_token_auth
端点提供用户名和密码来获取令牌。
Now, I should be able to access any API provided I add Authorization HTTP header in this format. 现在,只要以这种格式添加Authorization HTTP标头,我就应该能够访问任何API。 Authorization:Token "xyz" 授权:令牌“ xyz”
But I keep getting {"detail":"Authentication credentials were not provided"}
但是我不断收到{"detail":"Authentication credentials were not provided"}
Here is the view I am trying to access. 这是我尝试访问的视图。
class SubcategoryView(APIView):
def get(self,request):
serializer=SubcategorySerializer(Subcategory.objects.all(),many=True)
return JsonResponse({"subcategories":serializer.data})
Why do I keep getting this error? 为什么我不断收到此错误? What am I doing wrong? 我究竟做错了什么?
Remove the :
from the Authorization
header value . 从Authorization
标头值中删除:
。 The value of the Authorization
header should look like this: Authorization
标头的值应如下所示:
Token <token value>
You need to pass the Token as Header like Authorization: Token 1740...20
. 您需要像Authorization: Token 1740...20
一样将令牌作为标头传递Authorization: Token 1740...20
。 No semicolon after Token
. Token
后没有分号。 Here is a curl command 这是curl命令
curl -X GET http://127.0.0.1:8000/subcategory/ -H 'Authorization: Token 1740...20'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.