简体   繁体   English

DRF令牌身份验证:{“详细信息”:“未提供身份验证凭据。”}

[英]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.authtokenINSTALLED_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.

相关问题 DRF:“详细信息”:“未提供身份验证凭据。” - DRF: “detail”: “Authentication credentials were not provided.” DRF中未提供“身份验证凭据” - “Authentication credentials were not provided.” in DRF 令牌授权 Django {“详细信息”:“未提供身份验证凭据。”} - Token Authorization Django {“detail”:“Authentication credentials were not provided.”} Django:“详细信息”:“未提供身份验证凭据。” - Django : “detail”: “Authentication credentials were not provided.” "detail": "未提供身份验证凭据。" 一般视图 - "detail": "Authentication credentials were not provided." for general views Django Rest Framework {“detail”:“未提供身份验证凭据。”} - Django Rest Framework {“detail”:“Authentication credentials were not provided.”} 401 Unatuhorized(“详细信息”:“未提供身份验证凭据。”) - 401 Unatuhorized(“detail”:“Authentication credentials were not provided.”) JWT 注销“详细信息”:“未提供身份验证凭据。” - JWT Logout “detail”: “Authentication credentials were not provided.” &quot;detail&quot;: &quot;未提供身份验证凭据。&quot; 创建新用户时 - "detail": "Authentication credentials were not provided." When Creating a new user drf未提供身份验证凭据 - Authentication credentials were not provided drf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM