向 Spotify API 发出 PUT 请求时出现 401 错误

Getting 401 error while making a PUT request to the Spotify API

我正在尝试发出为用户创建播放列表的放置请求,但我一直收到状态:401,消息:“未提供令牌”错误。有人可以帮我看看我的代码并告诉我我做错了什么吗?

    const createNewPlaylist = () => {
    var user_id = ''
    axios.get(' https://api.spotify.com/v1/me',{

        headers:{
            'Authorization':"Bearer " + localStorage.getItem("accessToken")
        }

    }).then((response)=>{
        user_id = response.data.id
        console.log(localStorage.getItem("accessToken"))
        axios.post('https://api.spotify.com/v1/users/'+user_id+'/playlists',
        {
            headers:{
                'Authorization':"Bearer " + localStorage.getItem("accessToken")
            },
            data:{
                "name": "huhsss",
                "description": "New playlist description",
                "public": false
            }
        }

    ).then((res)=>{
            console.log(res)
    })

    })
}

我已经为这个特定的调用添加了所有必需的范围,我的令牌实际上适用于我发出的所有其他请求。它甚至适用于我发出的第一个 GET 请求。

对于axios post请求,data是第二个参数,选项(包括headers)是第三个 参数:

axios.post(
  "https://api.spotify.com/v1/users/" + user_id + "/playlists",
  {
    name: "huhsss",
    description: "New playlist description",
    public: false,
  },
  {
    headers: {
      Authorization: "Bearer " + localStorage.getItem("accessToken"),
    },
  }
);