Django:Rest框架validation标题

使用Django REST API,我试图validation我的请求。

这是我想发送的内容:

Content-Type: application/json, Authentication: token="6d82549b48a8b079f618ee9c51a6dfb59c7e2196" 

这是我回来的:

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

有人能给我正确的标题吗?

谢谢

标题:

 Accept: application/json Content-Type: application/json Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196 Connection: keep-alive Origin: chrome-extension: //rest-console-id User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 

在这里输入图像说明

Settings.py

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

view.py

 class ProfileList(generics.ListCreateAPIView): """ API endpoint that represents a list of users. """ permission_classes = (permissions.IsAuthenticated,) model = Profile serializer_class = ProfileSerializer def pre_save(self, obj): obj.owner = self.request.user 

假设你正在尝试使用TokenAuthentication,标题应该是这样的:

 Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196 

如文档中所述 。

以防万一谁遇到这个错误。 如果您使用mod_wsgi在Apache上运行Django,也会发生这种情况,因为授权头由mod_wsgi除去。 您需要将以下内容添加到VirtualHostconfiguration中:

WSGIPassAuthorization On

我与我的令牌authentication有同样的麻烦

这解决了我的问题

settings.py

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

在我的情况下,这工作:
(Django REST Framework v3)

settings.py

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

views.py

 class Test(APIView): def get(self, request, format=None): return Response({'Result': 'OK'}) 

urls.py

 router.add_api_view('test', url(r'^test/', views.Test.as_view(),name='test')) 

不要忘记在标题中发送令牌信息:

  Key: Authorization Value: Token 76efd80cd6849ad7d35e04f1cc1eea35bdc20294 

要生成令牌,可以使用以下代码(代码中的某处):

 from rest_framework.authtoken.models import Token user = User.objects.get(username='<username>') token = Token.objects.create(user=user) print(token.key) 

对于那些在AWS弹性beanstalk,你是一种卡住了Apache,除非你有

WSGIPassAuthorization On

正如@Fiver所提到的,你的头文件被删除了

我做了一个脚本来检查conf文件的最后一行是否是WSGIPassAuthorization On而不是我们更新它并重新启动服务器

在我的Django应用程序中,我的sh文件有一个configuration文件夹

CONFIGS /服务器/ update-apache.sh

 if [[ $(tac /etc/httpd/conf/httpd.conf | egrep -m 1 .) == $(echo 'WSGIPassAuthorization On') ]]; then echo "Httpd.conf has already been updated" else echo "Updating Httpd.conf.." echo 'WSGIPassAuthorization On' >> /etc/httpd/conf/httpd.conf service httpd restart fi 

在我把它提交给git之前使它成为可执行的

chmod +x configs/server/update-apache.sh

然后在我的python.config文件中,最后添加命令

.ebextensions / python.config

 ... ... container_commands: 01_migrate: command: "python manage.py migrate" leader_only: true 02_collectstatic: command: "python manage.py collectstatic --noinput" 03_change_perm: command: "chown -R wsgi:root static" 03_update_apache: command: "sh configs/server/update-apache.sh" 

现在任何启动的新机器都会检查服务器是否更新,如果需要更新