Spotify Web API returns 404 具有有效的节目 ID

Spotify Web API returns 404 with valid show id

我正在试用 WebAPI,但 运行 遇到了一个毫无意义的障碍。

在开发者控制台中搜索节目 ID 4rOoJ6Egrf8K2IrywzwOMk returns 的剧集列表。

如果我登录到我的 spotify 帐户,甚至可以在浏览器中访问给定的 URL (https://api.spotify.com/v1/shows/4rOoJ6Egrf8K2IrywzwOMk/episodes) returns 剧集列表。

我不确定我的代码中有什么问题。

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

此代码returns:

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

其他函数及变量设置:

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

您可能需要包含 market 参数。

我找到这个 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