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

Time:12-06

I am trying to make a put request to create a playlist for a user and I keep getting a status: 401, message: "No token provided" error. Can someone help me take a look at my code and tell me what I am doing wrong?

    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)
    })

    })
}

I have already added all the required scopes for this specific call and my token actually works for every other requests I make. It even works for the first GET request I make.

CodePudding user response:

For an axios post request, the data is the second argument and options (including headers) are the third argument:

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