繁体   English   中英

使用 fetch 时发布 https://accounts.spotify.com/api/token 415 Unsupported Media

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

我正在尝试使用 fetch 请求从 spotify api 获取授权代码,但我一直收到 415 错误代码。

class CodeHandler {
  constructor(Args){
    this._args = Args;
    this.opts = {
       data: {
         name: "test",
         public: "false",
     grant_type: 'authorization_code',
     code: this._args[1],
     redirect_uri: 'http%3A%2F%2Ftest.openjukeboxdev.info%2F'
       },
       headers: {
         'Authorization': 'Basic ZjRmMTk0MTIzOTM3NDgyMmI5Mjg3OGY4YTUzYjUwMjk6MDVlODA3ZDk0NDljNGE1MmFlMzM1YTQxOTlhMjMzYmI'
       }}
  }

  response() {
    console.log(this.opts)
    console.log(JSON.stringify(this.opts))
    return this._args[1];
  }

  getAccessToken(){
    const url = "https://accounts.spotify.com/api/token";

    fetch(url, {
      method: 'post',
      body: JSON.stringify(this.opts)
    })
   .then(function (data) {
      console.log('Request succeeded with JSON response', data);
    })
    .catch(function (error) {
      console.log('Request failed', error);
    }); 
  }
  pausePlayback(){
    const http = new XMLHttpRequest();
    const url = 
    http.open("POST", url);
    http.send();
    alert(http.responseText);
  }
}

这是完整的错误。 当我尝试提出发布请求时,我得到了。

Response {type: "cors", url: "https://accounts.spotify.com/api/token", redirected: false, status: 415, ok: false, …}
type: "cors"
url: "https://accounts.spotify.com/api/token"
redirected: false
status: 415
ok: false
statusText: ""
headers: Headers
__proto__: Headers
body: (...)
bodyUsed: false
__proto__: Response

415 建议您使用不正确的Content-Type标头发送数据。

415 - Unsupported Media是一个客户端错误,表示服务器拒绝接受请求,因为请求负载的格式不受支持。

可能的错误可能是由于错误的Content-TypeContent-Encoding 您的请求似乎是JSON格式。 尝试将Content-Type设置为application-json (如果还没有),看看是否能解决问题。

我添加了 "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" 并编码了我的 json 正文,现在一切正常

暂无
暂无

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

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