Search code examples
javascriptreact-nativeaxiosspotify

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


Here is the code that I am using:

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

        }
    )

I have already previously provided access to all the necessary scopes and gotten a corresponding authentication token, yet when I try to create a playlist with the code I provided, I get an error saying "Request failed with status code 401".

Nor does it work when I tried creating a playlist in the Spotify's own API site here: https://developer.spotify.com/console/post-playlists/ After clicking "try it" using their own example, nothing happens and no playlist is created for me.

Am I doing something royally wrong, or is it something that Spotify has to sort out?

Thanks!


Solution

  • Didn't try it but you can do following

        // 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);