【问题标题】:Getting 401 error while making a PUT request to the Spotify API向 Spotify API 发出 PUT 请求时出现 401 错误
【发布时间】:2022-01-10 19:57:30
【问题描述】:

我正在尝试发出 put 请求为用户创建播放列表,但我不断收到状态: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 请求。

【问题讨论】:

    标签: javascript reactjs api frontend spotify


    【解决方案1】:

    对于axios 发布请求,data 是第二个参数,选项(包括标题)是 第三个​​ 参数:

    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"),
        },
      }
    );
    

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2019-08-22
      • 2016-05-19
      • 2021-04-03
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      相关资源
      最近更新 更多