Spotify API帮助。 Response_type =代码无效,response_type = token有效吗?

时间:2019-08-08 12:51:22

标签: reactjs spotify

我正在尝试使用Spotify API,并且一直关注文档: https://developer.spotify.com/documentation/general/guides/authorization-guide/

我可以使用response_type = token使用“隐式授予”流程。但是,当我尝试使用response_type = code的授权码流程时,出现错误。我唯一的猜测是,我应该在后端运行它,因为目前它正在前端运行,并且我正计划将令牌传递给后端。

connect = () => {
    document.location.href = `${authEndpoint}response_type=token&client_id=${clientId}&scope=${scopes.join("%20")}&redirect_uri=${redirectUri}&show_dialog=true`;
  }

const hash = window.location.hash
  .substring(1)
  .split("&")
  .reduce(function (initial, item) {
    if (item) {
      var parts = item.split("=");
      console.log("DECODEEEE",parts)
      initial[parts[0]] = decodeURIComponent(parts[1]);
    }
    return initial;
  }, {});
使用response_type = code的

console.log输出:

GET https://api.spotify.com/v1/me/player 401
index.js:72 ===error Error: Request failed with status code 401
    at createError (createError.js:17)
    at settle (settle.js:19)
    at XMLHttpRequest.handleLoad (xhr.js:60)

console.log输出,使用response_type = token:

DECODEEEE (2) ["access_token", "REDACTED_ACCESS_TOKEN"]0: "access_token"1: "REDACTED_ACCESS_TOKEN"length: 2__proto__: Array(0)
index.js:22 DECODEEEE (2) ["token_type", "Bearer"]
index.js:22 DECODEEEE (2) ["expires_in", "3600"]

0 个答案:

没有答案