[英]Django Rest Framework JWT “Authentication credentials were not provided.”}
I am trying to write a simple API that uses JWT tokens to authenticate. 我正在尝试编写一个使用JWT令牌进行身份验证的简单API。
My settings.py
: 我的
settings.py
:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
'rest_framework.permissions.IsAdminUser'
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
'PAGE_SIZE': 10
}
And my View looks like this: 我的视图如下所示:
class ExampleView(APIView):
permission_classes = (IsAuthenticated,)
authentication_classes = (JSONWebTokenAuthentication,)
def get(self, request, format=None):
return JsonResponse({"username":str(request.user)})
Which really does nothing, but I don't even get there. 真的什么也没做,但是我什至没有到那里。 I have registered the JWT Token provider as in the documentation with
url(r'^api-token-auth/', obtain_jwt_token)
and I receive a token when I call this endpoint. 我已经在文档中使用
url(r'^api-token-auth/', obtain_jwt_token)
注册了JWT令牌提供程序,并且在调用此终结点时收到令牌。
However, when I then make a request against my APIView (which contains the header Authorization: JWT <my_token>
I always receive the following error: 但是,当我随后针对我的APIView(包含标头
Authorization: JWT <my_token>
发出请求时,我总是收到以下错误:
[Response]: 403 [Data]: {"detail":"Authentication credentials were not provided."}
What am I missing? 我想念什么? Thanks in advance!
提前致谢!
If you are using apache as web server then add the following in apache httpd.conf
file: 如果您将apache用作Web服务器,请在apache
httpd.conf
文件中添加以下内容:
WSGIPassAuthorization On
This worked for me. 这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.