我将尝试获取Spotify用户的公共播放列表,但我总是遇到401错误。
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1");
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 2");
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 3");
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
我使用此网址https://api.spotify.com/v1/users/id/playlists,其中id必须是Spotify的用户名。我有一个oAuth密钥。我使用这个JavaScript,JQuery代码请求API:
{
"error": {
"status": 401,
"message": "This request requires authentication."
}
}
答案 0 :(得分:1)
您需要将Authorization
和Host
值作为标题发送,而不是作为options对象的属性发送。另请注意,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
标题,因为我不相信它是必需的。