django-rest-auth 从 rest-auth/user 获取用户名

django-rest-auth get username from rest-auth/user

我想检索用户详细信息以显示 logged-in 用户的用户名 我需要从 django-rest-auth 的“http://127.0.0.1:8000/rest-auth/user/”中获取用户名 我是 reactjs 的新手,尝试了成功但无法通过的身份验证。

到目前为止我已经试过了

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

哪个returns禁止的403错误;

错误

Error: Request failed with status code 403
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:61)

同样在上面的代码中,我还通过以下方式指定了 headers headers: { 'Authorization': "token key_from_DRF " } 但运气不好

这个我也试过了

axios.get(`http://127.0.0.1:8000/rest-auth/user/`,
            {
                headers: { 'Content-Type': 'application/json' }
            }
        )
            .then(res => {
                console.log(res)
            }).catch(Error => {
                console.log(Error)
            })

which returns 和之前一样的错误。 我该如何成功执行这个请求?

axios POST 方法是正确的,但是请确保您传递了令牌

let tokentoserver = localStorage.getItem('token');
console.log(tokentoserver);

axios.get(`http://127.0.0.1:8000/rest-auth/user/`,
            {
                headers: { 'Authorization': "Token " tokentoserver }
            }
        ).then(res => {
            console.log(res)
        }).catch(Error => {
            console.log(Error)
        })

我已经删除了你用来一起添加令牌的 + 标志