繁体   English   中英

使用 fetch 时 POST https://accounts.spotify.com/api/token 415 错误

[英]POST https://accounts.spotify.com/api/token 415 error when using fetch

我正在尝试使用 fetch 请求从 spotify api 获取授权代码,但我不断收到 415 错误代码。 当我最初使用 $.ajax 而不是 fetch 时,我没有任何错误。

let client_id = '8f10fa8af1aa40c6b52073811460bf33'
let client_secret = '27a7c01912444b409a7f9a6d1f700868'
let ah = btoa(client_id + ":" + client_secret)

const getAuthToken = (searchedTerm) => {
fetch( `https://accounts.spotify.com/api/token`,
  {
    headers: {
      'Content-Type': 'application/x-www-form-url-encoded',
      'Authorization': `Basic ${ah}`
    },
    body: {
      grant_type: 'client_credentials'
    },
    json: true,
    method : "POST"
  }
)

  .then(function(response) { 
    authToken = response.access_token;
    spotifySearch(response.access_token, searchedTerm);  
  })
}

在类似的帖子中查看答案。 请注意,他们在那里设置了'Content-Type':'application/x-www-form-urlencoded'urlencoded之间没有连字符 我认为你只需要改变

    headers: {
      'Content-Type': 'application/x-www-form-url-encoded',
      'Authorization': `Basic ${ah}`
    },

    headers: {
      'Content-Type': 'application/x-www-form-urlencoded', // no hyphen in urlencoded
      'Authorization': `Basic ${ah}`
    },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM