我正在尝试在我的服务器API上实现基于令牌的授权。但是,当我触发一个查询时,它会返回{"detail": "Authentication credentials were not provided."}
我几乎完成了在各种帖子和Django文档中推荐的所有设置。
在我的settings.py
INSTALLED_APPS = [
'rest_framework.authtoken',
]还有,
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}在我的视图文件中:
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
class UserList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]和我在WSGI的Apache配置文件中
WSGIPassAuthorization On
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP-AUTHORIZATION:%1]我不知道除了这个我还错过了什么。令牌是在超级用户级别使用命令行预先生成的。
发布于 2021-02-21 13:46:30
当您在浏览器中访问您的网站时看到的默认Django Rest框架HTML不支持令牌授权。这是因为您需要在发送请求时将Authorization: Token <insert your token here>放入头中。这是一个问题,因为您填写的表单没有地方更改页眉。
我建议使用Postman (很好的GUI)或curl (命令)来测试您的API,因为您需要编辑请求的HTTP头。
发布于 2021-03-01 12:55:24
你看到this related S.O. question了吗?
如果您使用的是mod_wsgi,请省略重写部分,将WSGIPassAuthorization On放在WSGIScriptAlias、WSGIDaemonProcess和WSGIProcessGroup指令下。刚刚让我的Apache + mod_wsgi配置正常工作,看起来像这样:
...
WSGIScriptAlias / /var/www/html/base/wsgi.py
WSGIDaemonProcess django_app python-path=/var/www/html python-home=/var/www/html/env
WSGIProcessGroup django_app
WSGIPassAuthorization On
...https://stackoverflow.com/questions/66299178
复制相似问题