带有 Axios GET 请求的 Spotify Recommendations API 返回 400“仅支持有效的承载身份验证”

问题描述

我按照文档步骤使用 POST 请求检索访问令牌,如下所示:

import qs from 'qs';

export const getAuth = async () => {
  const clientId = process.env.REACT_APP_CLIENT_ID;
  const clientSecret = process.env.REACT_APP_CLIENT_SECRET;
  
  const headers = {
    headers: {
      Accept: 'application/json','Content-Type': 'application/x-www-form-urlencoded',},auth: {
      username: clientId,password: clientSecret,};
  const data = {
    grant_type: 'client_credentials',};

  try {
    const response = await axios.post(
      'https://accounts.spotify.com/api/token',qs.stringify(data),headers
    );
    return response.data.access_token;
  } catch (error) {
    console.log(error);
  }
};

此令牌适用于获取曲目信息等基本功能,但是当我尝试访问推荐端点时,我得到一个 error 400 "Only valid bearer authentication supported"

这是我的 GET 请求:

 axios({
      url: 'https://api.spotify.com/v1/recommendations',method: 'GET',headers: {
        "Authorization": "Bearer " + this.state.token,"Accept": "application/json","Content-Type": "application/json",params: {
        "seed_artists": "20wkVLutqVOYrc0kxFs7rA","seed_genres": "indie-pop","seed_tracks": "6Nle9hKrkL1wQpwNfEkxjh"
      }
    }).then(function(response) {
        console.log(response);
    }).catch(function(error) {
    });

这是 chrome 中的请求 URL:https://api.spotify.com/v1/recommendations?seed_artists=20wkVLutqVOYrc0kxFs7rA&seed_genres=indie-pop&seed_tracks=6Nle9hKrkL1wQpwNfEkxjh

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)