使用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
假设你正在尝试使用TokenAuthentication,标题应该是这样的:
Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196
如所描述的文档中 。
万一别人遇到这个错误。 如果你是在Apache使用mod_wsgi的,因为授权头被mod_wsgi的剥离出去跑的Django也会发生这种情况。 你需要以下添加到您的虚拟主机配置:
WSGIPassAuthorization On
我跟我的令牌认证同样的烦恼
这修正了问题我
settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAdminUser'
),
'PAGINATE_BY': 10,
}
在我的情况下,这个工作:
(Django的REST框架第三版)
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弹性青苗,你是那种卡与Apache,除非你有
WSGIPassAuthorization On
正如@Fiver提到了你的头获得剥夺
不用手动修复此,使一个新的形象,我做了一个脚本来检查,如果conf文件的最后一行是WSGIPassAuthorization On
,如果它不是我们更新并重新启动服务器
在我的Django应用程序我有一个config文件夹和我的SH文件
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
让它excecutable之前,我承诺它与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"
现在,启动任何新机将有一个做检查,看看是否在服务器更新,这样做,如果需要的话