简体   繁体   English

通过用户名从Spotify获取公共播放列表

[英]Get public playlist from Spotify by username

I'll try to get the public playlists of an Spotify-user, but I get always a 401-error. 我将尝试获取Spotify用户的公共播放列表,但始终会出现401错误。

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

I use this url https://api.spotify.com/v1/users/id/playlists where id must be an username from Spotify. 我使用此URL https://api.spotify.com/v1/users/id/playlists ,其中id必须是Spotify的用户名。 I have an oAuth key. 我有一个oAuth密钥。 I request the API with this JavaScript, JQuery code: 我使用以下JavaScript,JQuery代码请求API:

$.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>");
    }
});

You need to send the Authorization and Host values as headers, not as properties of the options object. 您需要将AuthorizationHost值作为标头发送,而不是作为options对象的属性发送。 Also note that accepts should be lower case. 另请注意, accepts应为小写。 Try this: 尝试这个:

$.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>");
    }
});

You could probably also leave out the Host header, as I don't believe it's required. 您可能还可以省略Host标头,因为我认为这不是必需的。

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

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