gpt4 book ai didi

javascript - 如何使用我的 spotify api token ?

转载 作者:行者123 更新时间:2023-11-30 21:06:42 26 4
gpt4 key购买 nike

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

我的问题是我现在该如何使用这个 token ?我又遇到了 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 项目),我一直在学习教程,当时他们不必处理授权。任何帮助或资源表示赞赏。谢谢。

最佳答案

访问 token 必须在 header 中发送:

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'
}
});

关于javascript - 如何使用我的 spotify api token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46531907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com