简体   繁体   English

drf未提供身份验证凭据

[英]Authentication credentials were not provided drf

when i trying to access api i'm getting this error: 当我试图访问api时,我收到此错误:

 "detail": "Authentication credentials were not provided."

i have included this in settings.py: 我已将其包含在settings.py中:

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':(
    'rest_framework.authentication.TokenAuthentication',
    'rest_framework.authentication.SessionAuthentication',


),
'DEFAULT_PERMISSION_CLASSES':(
    'rest_framework.permissions.IsAuthenticated',
)

}

my app api urls.py: 我的app api urls.py:

from django.urls import path,include
from . import views
from rest_framework import routers

router = routers.SimpleRouter()
router.register(r'',views.UserViewSet, 'user_list')
urlpatterns = router.urls

my views.py: 我的views.py:

class UserViewSet(viewsets.ModelViewSet):
       queryset = User.object.all()
       serializer_class = serializers.UserSerializers

serializers.py: serializers.py:

from rest_framework import serializers
from users.models import User


class UserSerializers(serializers.ModelSerializer):
  class Meta:
    model = User
    fields = ('email','password')

my main urls.py: 我的主要urls.py:

urlpatterns = [
path('admin/', admin.site.urls),
path('',include(urls)),
path ('', include(user_urls)),
path('api/',include(api_urls)),

when i running localhost:8000/api i'm getting the error 当我运行localhost:8000 / api我收到错误

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES':(
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES':(
        'rest_framework.permissions.IsAuthenticated',
    )
}

use this in your settings.py file, what is happening is that rest_framework.authentication.TokenAuthentication expects a authorization header with token as it's value, but you can't send that with your browser, to browse API from browser you must have SessionAuthentication enabled. settings.py文件中使用它,发生的事情是rest_framework.authentication.TokenAuthentication需要一个带有令牌的授权标头作为它的值,但你无法通过浏览器发送,要从浏览器浏览API,你必须启用SessionAuthentication

You can't access the api from the browsers url if you are using TokenAuthentication . 如果使用TokenAuthentication,则无法从浏览器URL访问api。 as said by @DarkOrb TokenAuthentication expects a authorization header with token as it's value. 正如@DarkOrb所说, TokenAuthentication需要一个带有令牌的授权标头作为它的值。 So You must pass token whenever you call the api . 因此,无论何时调用api,都必须传递令牌 You can test your api using postman . 你可以用邮递员测试你的api。

在此输入图像描述

In above image i have passed token in headers of postman to access my api. 在上面的图像中,我已经在邮递员的标题中传递了令牌来访问我的api。 When you call your api from frontend side,pass your token along with the request. 当您从前端呼叫您的api时,请将您的令牌与请求一起传递。 If you just want to use your api in only desktop's browser,in that case you can use SessionAuthentication only.For mobile devices Tokenauthentication must be done. 如果您只想在桌面浏览器中使用api,那么您只能使用SessionAuthentication。对于移动设备,必须完成Tokenauthentication

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM