Ken Ingram
Ken Ingram

Reputation: 1608

Spotify Web API returns 404 with valid show id

I'm trying out the WebAPI and have run into a snag that doesn't make sense.

In the developer console a search for the episode list for show id 4rOoJ6Egrf8K2IrywzwOMk returns a list.

Even accessing the given URL (https://api.spotify.com/v1/shows/4rOoJ6Egrf8K2IrywzwOMk/episodes) in the browser returns the episode list, if I'm logged into my spotify account.

I'm not sure what's broken in my code.

async function listPodcastEpisodes(id) {
  const access_token = await getAuth()
  const api_url = `https://api.spotify.com/v1/shows/${id}/episodes`
  console.log(api_url)
  try {
    const response = await axios.get(api_url, setAxiosOptions(access_token))
    return response.data
  } catch(err) {
    console.log(`Error: ${JSON.stringify(err?.response?.data, null, 2)}`)
  }
}

listPodcastEpisodes(process.argv[2])
  .then(x => console.log(x))

This code returns:

$ node getInfo.js 4rOoJ6Egrf8K2IrywzwOMk
https://api.spotify.com/v1/shows/4rOoJ6Egrf8K2IrywzwOMk/episodes
Error: {
  "error": {
    "status": 404,
    "message": "non existing id"
  }
}
undefined

The other functions and variable settings:

const auth = `${process.env.ID}:${process.env.SECRET}`
const auth_encoded = Buffer.from(auth, 'utf-8').toString("base64")

function setAxiosOptions(data) {
  return {
    headers: {
      'Authorization': `Bearer ${data}`,
      'Content-Type': 'application/json'
    }
  }
}

async function getAuth () {
  try {
    const token_url = 'https://accounts.spotify.com/api/token';
    const data = qs.stringify({'grant_type':'client_credentials'});
    const response = await axios.post(token_url, data, {
      headers: { 
        'Authorization': `Basic ${auth_encoded}`,
        'Content-Type': 'application/x-www-form-urlencoded' 
      }
    })

    return response.data.access_token;
  } catch(error) {
    console.log(error);
  }
}

Upvotes: 1

Views: 1045

Answers (1)

Montgomery Watts
Montgomery Watts

Reputation: 4034

It looks like you may need to include the market parameter.

I found this GitHub issue on the official Web API repo describing the same problem you're experiencing. This issue is referenced by this issue, where GitHub user MrXyfir posts their workaround:

Edit: For anyone else who ends up with this issue, the solution for me was to set the market param. https://api.spotify.com/v1/shows/4rOoJ6Egrf8K2IrywzwOMk/episodes?market=US

Upvotes: 4

Related Questions