Django Rest框架 - 未提供验证凭据 [英] Django Rest Framework - Authentication credentials were not provided

查看:368
本文介绍了Django Rest框架 - 未提供验证凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django Rest Framework开发一个API。我正在尝试列出或创建一个Order对象,但是当我尝试访问控制台时,会出现以下错误:

  {detail:没有提供身份验证凭证} 

从django.shortcuts导入render 
从rest_framework导入viewets
从django。 contrib.auth.models import来自rest_framework.renderers的用户
导入JSONRenderer,来自rest_framework.response的YAMLRenderer
导入响应
从rest_framework.views导入APIView
from order.models import *
from API.serializers import *
from rest_framework.permissions import IsAuthenticated

class OrderViewSet(viewsets.ModelViewSet):
model = Order
serializer_class = OrderSerializer
permission_classes =(IsAuthenticated)

序列化器

  class OrderSerializer(serializers.H yperlinkedModelSerializer):

class Meta:
model = Order
fields =('field1','field2')

我的网址

  # -  *  - 编码:从django.conf.urls导入模式的utf-8  -  *  -  
包含来自django.conf的url
导入设置
from django.contrib import admin
from django.utils.functional import curry
from django.views.defaults import *
from rest_framework import routes
from API.views import *

admin。 autodiscover()

handler500 =web.views.server_error
handler404 =web.views.page_not_found_error

router = router.DefaultRouter()
router.register(r'orders',OrdersViewSet)

urlpatterns = patterns('',
url(r'^ api-auth /',include('rest_framework.urls' ,namespace ='rest_framework')),
url(r'^ api-token-auth /','rest_framework.authtoken.views.obtain_auth_token' ),
url(r'^ api /',include(router.urls)),

然后我在控制台中使用这个命令:

  curl -X GET http: /127.0.0.1:8000/api/orders/ -H'授权:令牌12383dcb52d627eabd39e7e88501e96a2sadc55'

而且错误说:

  {detail:未提供身份验证凭据} 


解决方案

通过将DEFAULT_AUTHENTICATION_CLASSES添加到我的settings.py解决方案

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


I'm developing an API using Django Rest Framework. I'm trying to list or create an "Order" object, but when i'm trying to access the console gives me this error:

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

Views:

from django.shortcuts import render
from rest_framework import viewsets
from django.contrib.auth.models import User
from rest_framework.renderers import JSONRenderer, YAMLRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from order.models import *
from API.serializers import *
from rest_framework.permissions import IsAuthenticated

class OrderViewSet(viewsets.ModelViewSet):
    model = Order
    serializer_class = OrderSerializer
    permission_classes = (IsAuthenticated,)

Serializer:

class OrderSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Order
        fields = ('field1', 'field2')

And my URLs:

# -*- coding: utf-8 -*-
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
from django.utils.functional import curry
from django.views.defaults import *
from rest_framework import routers
from API.views import *

admin.autodiscover()

handler500 = "web.views.server_error"
handler404 = "web.views.page_not_found_error"

router = routers.DefaultRouter()
router.register(r'orders', OrdersViewSet)

urlpatterns = patterns('',
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),
    url(r'^api/', include(router.urls)),
)

And then I'm using this command in the console:

curl -X GET http://127.0.0.1:8000/api/orders/ -H 'Authorization: Token 12383dcb52d627eabd39e7e88501e96a2sadc55'

And the error say:

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

解决方案

Solved by adding "DEFAULT_AUTHENTICATION_CLASSES" to my settings.py

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

这篇关于Django Rest框架 - 未提供验证凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆