我是django rest framework jwt token包。由于某种原因,它没有收到 token 凭据。我确定我正在以正确的格式使用 axios 来存储 token 。我不确定错误是前端还是后端。我在后端使用 pythonanywhere。
设置.py
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}
前端
handleFormSubmit(e) {
e.preventDefault();
if (this.getValidationState() == 'error') {
this.setState({invalidFormSubmit: true})
return;
}
axios.defaults.headers.common['Authorization'] = 'Token ' + this.props.auth.getToken();
console.log(this.props.auth.getToken());
var capsule = new MyForm();
capsule.append("user", this.state.userUrl);
capsule.append("dateToPost", this.state.dateToPost.format());
capsule.append("image", this.state.image);
capsule.append("caption", this.state.caption);
capsule.append("dateToDelete", this.state.dateToPost.add(1, "d").format());
axios.post(this.props.auth.domain + "/snapcapsule/", capsule)
.then(() => {
this.setState({redirect: true})
}).catch(err =>{
alert(err);
})
}
错误
{"detail":"未提供身份验证凭据。"
最佳答案
settings.py 中的 token 验证
JWT_AUTH = {
# Authorization:Token xxx
'JWT_AUTH_HEADER_PREFIX': 'Token',
}
默认的 JWT_AUTH_HEADER_PREFIX
是 JWT。
JWT_AUTH_HEADER_PREFIX You can modify the Authorization header value prefix that is required to be sent together with the token. The default value is JWT. This decision was introduced in PR #4 to allow using both this package and OAuth2 in DRF.
Another common value used for tokens and Authorization headers is Bearer.
Default is JWT.
文件是here .
关于javascript - Django 休息框架 : HTTP 401 Unauthorized error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51585240/