0

Helo, I'm trying to code a React app which gets current song from Spotify and plays/pauses the song. However I can't pause or play the current song.

  const pauseURL = 'https://api.spotify.com/v1/me/player/pause'

  const [token, setToken] = useState('')
  const [player, setPlayer] = useState({})

  useEffect(() => {
    if (localStorage.getItem('accessToken')) {
      setToken(localStorage.getItem('accessToken'))
    }
  }, [token])

  const onPause = () => {
    axios.put(pauseURL, {
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        Authorization: 'Bearer' + token,
      },
    })
  

This is the pause function, and gets the correct token 100%. It always gives 401 error. But it gets the necessary token. When I try it in Postman with the token it pauses or plays the song.

  useEffect(() => {
    getCurrentInfo()
  }, [token, user])

  return (
        <Image
          onClick={() => onPause()}
          className="circlePhoto"
          src={data.item.album.images[1].url}
        />
   

1 Answers1

0

Hi Coding Kitty I too dosent had much knowledge on spotify api connections. The way is to see some of the you tube tutorials on spotify clone channels like

  1. Sonny sangha (https://www.youtube.com/watch?v=3xrko3GpYoU&t=90s)
  2. Clever programmer