javascript - Django 休息框架 : HTTP 401 Unauthorized error

标签 javascript python django http axios

我是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/

相关文章:

python - 谷歌应用引擎代理

python - 尝试通过 For 循环显示数据库中每个用户的 amount_won 总额

javascript - JavaScript 如何检测正则表达式?

python - 关于分帧算法/提取Python中某个感兴趣的对象

javascript - 将对象数组过滤到新数组中

python - 快速替换 pandas.Series of datetime 的 tzinfo

django - 为什么 Get 和 Filter 给出不同的结果? ( Django )

python - 使用 Django,为什么 REMOTE_ADDR 会在 Web 服务器上返回 127.0.0.1?

javascript - 使用 React 过滤对象列表

javascript - 访问多个 JSON 数组中的元素