django restframework令牌身份验证失败,返回“0”;无效令牌";
我对令牌身份验证有问题。 我用django内置服务器运行django应用程序django restframework令牌身份验证失败,返回“0”;无效令牌";,django,authentication,django-rest-framework,jwt,Django,Authentication,Django Rest Framework,Jwt,我对令牌身份验证有问题。 我用django内置服务器运行django应用程序 $python manage.py runserver 我的应用程序的URL.py from rest_framework_jwt.views import obtain_jwt_token from .views import LectureCreateView urlpatterns = [ ... url(r'^api/user_auth/$', obtain_jwt_token),
$python manage.py runserver
我的应用程序的URL.py
from rest_framework_jwt.views import obtain_jwt_token
from .views import LectureCreateView
urlpatterns = [
...
url(r'^api/user_auth/$', obtain_jwt_token),
url(r'^api/lecture/create/$', LectureCreateView.as_view()),
]
我的应用程序的models.py
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
class LectureStartView(APIView):
permission_classes = (IsAuthenticated,)
authentication_classes = (TokenAuthentication,)
def post(self, request):
...
和设置.py
...
INSTALLED_APPS = [
...
# Rest framework
'rest_framework',
'rest_framework.authtoken',
'myApp',
]
...
REST_FRAMEWORK = {
# other settings...
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
}
我要带令牌的身份验证。
我成功地发行了代币
发布“…api/user\u auth/”{
“用户名”:“测试”,
“密码”:“blahbalh123”
}
但我用令牌验证失败
请求:
POST ...api/lecture/create/ HTTP/1.1
Host: 127.0.0.1:8000
Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjIwMTMyMzA2Iiwib3JpZ19pYXQiOjE1MDk5NzA5NjcsInVzZXJfaWQiOjMsImVtYWlsIjoiaW50ZXJydXBpbmdAbmF2ZXIuY29tIiwiZXhwIjoxNTA5OTcxNTY3fQ.acwqAP4sBPZWYPC0GfgL3AZarNz4Opb_5P4RewZJYrI
答复:
Status: 401 Unauthorized
Allow →GET, POST, HEAD, OPTIONS
Content-Length →27
Content-Type →application/json
Date →Mon, 06 Nov 2017 12:59:17 GMT
Server →WSGIServer/0.1 Python/2.7.13
Vary →Accept
WWW-Authenticate →Token
X-Frame-Options →SAMEORIGIN
{
"detail": "Invalid token."
}
我的代码怎么了?
对不起,我的英语水平太差了。我认为您是在混合使用django rest framework和rest framework JWT的标记 在DJR文件中说: 来自rest\u framework.authtoken导入视图的
URL模式+=[
url(r'^api令牌身份验证/',视图。获取身份验证令牌)
]
您应该将代码替换为:
来自rest\u framework.authtoken导入视图的
from.views导入到createview
URL模式=[
...
url(r“^api/user\u auth/$”,视图。获取\u auth\u令牌),
url(r“^api/TEACH/create/$”,讲师创建视图.as_view()),
]
我希望它能对您有所帮助。我认为您混合了django rest framework和rest framework JWT的令牌 在DJR文件中说: 来自rest\u framework.authtoken导入视图的
URL模式+=[
url(r'^api令牌身份验证/',视图。获取身份验证令牌)
]
您应该将代码替换为:
来自rest\u framework.authtoken导入视图的
from.views导入到createview
URL模式=[
...
url(r“^api/user\u auth/$”,视图。获取\u auth\u令牌),
url(r“^api/TEACH/create/$”,讲师创建视图.as_view()),
]
我希望它能帮助你
Status: 401 Unauthorized
Allow →GET, POST, HEAD, OPTIONS
Content-Length →27
Content-Type →application/json
Date →Mon, 06 Nov 2017 12:59:17 GMT
Server →WSGIServer/0.1 Python/2.7.13
Vary →Accept
WWW-Authenticate →Token
X-Frame-Options →SAMEORIGIN
{
"detail": "Invalid token."
}