尝试使用 API 在 Spotify 中创建播放列表时出现 "Request failed with status code 401" 错误

Getting "Request failed with status code 401" error when trying to create a playlist in Spotify using their API

这是我正在使用的代码:

    await axios.post(arg.url,
        {
            headers: {
                "Content-Type": "application/json",
                Authorization: `Bearer ${SPYauthToken}`
            },
            params: {name: "playlist"},

        }
    )

我之前已经提供了对所有必要范围的访问权限并获得了相应的身份验证令牌,但是当我尝试使用我提供的代码创建播放列表时,我收到一条错误消息 "Request failed with status code 401"。

当我尝试在 Spotify 自己的 API 网站上创建播放列表时,它也不起作用:https://developer.spotify.com/console/post-playlists/ 使用他们自己的示例单击 "try it" 后,没有任何反应,也没有播放列表为我创造。

我是不是做错了什么,还是 Spotify 必须解决这个问题?

谢谢!

    await axios.post(arg.url,
        {
            headers: {
                "Content-Type": "application/json",
                "Authorization": `Bearer ${SPYauthToken}`
            },
            params: {name: "playlist"},

        }
    )

您没有在授权周围使用引号

我使用 fetch 修复了它:

    const data = { name: 'playlist' };

    fetch(arg.url, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          "Authorization": `Bearer ${SPYauthToken}`
        },
        body: JSON.stringify(data),
      })
      .then((response) => response.json())

完美运行。 Axios 似乎无法使用 POST,至少不像预期的那样,所以我可能会停止使用它。

感谢所有回答的人。

没有尝试过,但您可以按照以下步骤操作

    // request data object
    const data = {
        name: "playlist"
    };

    // set the headers
    const config = {
       headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${SPYauthToken}`
        }
    };

await axios.post(arg.url, data, config);
// maybe you need to stringify data so
await axios.post(arg.url, JSON.stringify(data), config);