Spotify 网站 API Ajax

Spotify Web API Ajax

我正在做一个小的副项目,以更好地使用网站 APIs 和 jQuery。不幸的是我做得不好。我想做的是使用 Spotify Web API 获取我的 3 个播放列表并将它们放在网页上。

我目前有:

var accessToken = "{my-token}";
$.ajax({
    url: 'https://api.spotify.com/v1/users/{my-username}/playlists?limit=3&offset=0',
    type: 'GET',
    headers {
        'Authorization' : 'Bearer ' + accessToken;
    },
    success: function(data) {
        console.log(data);
    }
});

我收到以下错误:

Failed to load resource: the server responded with a status of 401 (Unauthorized)

有没有人知道如何做到这一点。在我弄清楚之后,我将为未来不熟悉使用 jQuery 访问 Web APIs

的任何人创建一个快速操作方法

Spotify 的 Web API return HTTP 状态代码 401 未授权 (Error code documentation) 当访问令牌已过期或访问令牌无效时。打印出错误响应的正文将为您提供有关 API return 为什么出现 401 的进一步线索。

访问令牌将在一小时后过期,但可以根据您检索它的方式进行刷新。您可以在 Github 上的 Authorization Guide, or check out the web-api-auth-examples 项目中阅读有关检索访问令牌的不同方法的更多信息。