【问题标题】:Django cannot get the current user using rest-auth/user/Django 无法使用 rest-auth/user/ 获取当前用户
【发布时间】:2019-07-10 02:55:30
【问题描述】:

我正在使用带有 React 的 Django,我可以使用 http://127.0.0.1:8000/rest-auth/login/' 登录用户并使用 http://127.0.0.1:8000/rest-auth/registration/ 注册用户

我正在尝试获取当前用户:

axios.get('http://127.0.0.1:8000/rest-auth/user/')
    .then(res => {
        console.log(res)
    })

但我收到以下错误:

GET http://127.0.0.1:8000/rest-auth/user/ 403 (Forbidden)

我也在邮递员上试过,得到以下结果:

{
    "detail": "Authentication credentials were not provided."
}

有人可以告诉我正确的方法吗?提前致谢。

【问题讨论】:

  • 您能否检查请求并查看会话 cookie 是否与请求一起发送?

标签: django django-rest-framework


【解决方案1】:

这是因为你的路由需要Authorization token

axios.get('http://127.0.0.1:8000/rest-auth/user/, {
       headers: {'Authorization': "Bearer " + token}
    })
    .then(res => {
        console.log(res)
    })

【讨论】:

  • 你能解释一下这里的“Bearer”是什么意思吗?
  • 了解Bearer,关注this link
猜你喜欢
  • 2020-10-03
  • 2023-04-04
  • 2020-08-05
  • 2016-02-04
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 2022-01-17
相关资源
最近更新 更多