【问题标题】:Get public playlist from Spotify by username通过用户名从 Spotify 获取公共播放列表
【发布时间】:2016-03-11 04:21:44
【问题描述】:

我会尝试获取 Spotify 用户的公开播放列表,但我总是收到 401 错误。

{
    "error": {
        "status": 401,
        "message": "This request requires authentication."
    }
}

我使用这个网址https://api.spotify.com/v1/users/id/playlists,其中 id 必须是来自 Spotify 的用户名。我有一个 oAuth 密钥。我用这个 JavaScript 请求 API,JQuery 代码:

$.ajax({
    url: "https://api.spotify.com/v1/users/" + $("#username").val() + "/playlists",
    Authorization: "Bearer my_OAuth_Token",
    Host: "api.spotify.com",
    Accept: "application/json",
    type: "GET",
    success: function (data){

        var count = data.items.length;

        for (var i = 0; i < aantal; i++) {
            $("#playlists").append('<option>' + data.items[i].name + '</option>');
        }
    },
    error: function (data) {
        $("#playlists").append("<option>error</option>");
    }
});

【问题讨论】:

    标签: javascript jquery api spotify


    【解决方案1】:

    您需要将AuthorizationHost 值作为标头发送,而不是作为选项对象的属性。另请注意,accepts 应为小写。试试这个:

    $.ajax({
        url: "https://api.spotify.com/v1/users/" + $("#username").val() + "/playlists",
        headers: {
            Authorization: "Bearer my_OAuth_Token",
            Host: "api.spotify.com"
        },
        accepts: "application/json",
        type: "GET",
        success: function (data) {    
            var count = data.items.length;    
            for (var i = 0; i < aantal; i++) {
                $("#playlists").append('<option>' + data.items[i].name + '</option>');
            }
        },
        error: function (data) {
            $("#playlists").append("<option>error</option>");
        }
    });
    

    您也可以省略 Host 标头,因为我认为它不是必需的。

    【讨论】:

      猜你喜欢
      • 2021-02-11
      • 1970-01-01
      • 2011-09-23
      • 2011-09-23
      • 2014-10-28
      • 2015-08-31
      • 2016-12-26
      • 2019-07-27
      • 1970-01-01
      相关资源
      最近更新 更多