使用Django REST API,我正在尝试验证我的请求。
这就是我要发送的内容:
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
答案 0 :(得分:113)
以防万一其他人遇到此错误。如果您使用mod_wsgi在Apache上运行Django,也会发生这种情况,因为mod_wsgi会剥离授权标头。您需要将以下内容添加到VirtualHost配置中:
WSGIPassAuthorization On
答案 1 :(得分:26)
假设您尝试使用TokenAuthentication,标题应如下所示:
Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196
如上所述in the documentation。
答案 2 :(得分:19)
我的令牌认证遇到了同样的问题
这解决了我的问题
settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAdminUser'
),
'PAGINATE_BY': 10,
}
答案 3 :(得分:7)
在我的情况下,这有效:
(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)
答案 4 :(得分:2)
对于那些使用AWS弹性beanstalk并且你有点困难的人,除非你有
WSGIPassAuthorization On
如@Fiver所述,您的标题会被删除
我创建了一个脚本来检查conf文件的最后一行是WSGIPassAuthorization On
而不是我们更新它并重新启动服务器
在我的Django应用程序中,我有一个带有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文件中,我在末尾添加命令
...
...
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"
现在任何启动的新计算机都会检查服务器是否已更新,如果需要则更新