【问题标题】:How do I use my spotify api token?如何使用我的 spotify api 令牌?
【发布时间】:2018-03-13 21:38:54
【问题描述】:

所以我正在尝试使用 Spotify API 构建一个随机播放列表生成器,当我从他们的服务器获取信息时,它给了我一个 401 代码。我遵循了有关如何获取访问令牌的教程,现在我有了它。

我的问题是我现在如何使用这个令牌?我再次收到 401 错误,但我认为是因为我不知道如何订购 url?

JS/html:

const app = {};

app.apiUrl = 'https://api.spotify.com/v1';
var accessToken = '[private_info]';

//Allow the user to enter some names
app.events = function() {
    $('form').on('submit', function(e) {
      e.preventDefault();
      let artists = $('input[type=search]').val();
      artists = artists.split(',');
      let search = artists.map(artistName => app.searchArtist(artistName));
      console.log(search);

    });

};

//Go to spotify and get the artists
app.searchArtist = (artistName) => $.ajax({
    url: `${app.apiUrl}/search/` + accessToken,
    method: 'GET',
    dataType: 'json',
    data: {
        q: artistName,
        type: 'artist'
    }
});

//With the ids we want to get albums

//Then get tracks

//Then build playlist

app.init = function() {
    app.events();

};

$(app.init);
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Spotify Playlist Generator</title>
	<link rel="stylesheet" href="style.css">
</head>
<body>
	<main class="main-container">
		<section>
			<div class="form">
				<img src="images/note.svg" alt="">
				<form action="">
					<input type="search" value="">
					<input type="submit" value="Create">
				</form>
				<p>Icon created by unlimicon from the Noun Project</p>
			</div>
			<div class="playlist">
				<div class="loader">
					<div class="inner-circle"></div>
				</div>
			</div>
		</section>
	</main>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
	<script src="script.js"></script>
</body>
</html>

我还是 js/ajax 的新手(这是我的第一个 API 项目),我一直在学习一个教程,当时他们不需要处理授权。任何帮助或资源表示赞赏。谢谢。

【问题讨论】:

    标签: javascript html json ajax spotify


    【解决方案1】:

    访问令牌必须在头部信息内:

    curl -X GET "https://api.spotify.com/v1/search?q=Muse&type=track,artist&market=US" -H "Accept: application/json" -H "Authorization: Bearer myToken"

    app.apiUrl = 'https://api.spotify.com/v1';
    var accessToken = '[private_info]';
    
    //Go to spotify and get the artists
    app.searchArtist = (artistName) => $.ajax({
        url: `${app.apiUrl}/search`,
        headers: {
            'Authorization':'Bearer ' + accessToken
        },
        method: 'GET',
        dataType: 'json',
        data: {
            q: artistName,
            type: 'artist'
        }
    });
    

    【讨论】:

    • 实际上了它。原来我不得不刷新我的访问令牌。再次感谢!!!! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2017-02-14
    • 2021-01-26
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多