简体   繁体   English

如何使用我的 Spotify api 令牌?

[英]How do I use my spotify api token?

So I'm trying to build a random playlist generator using the Spotify API and as I get the info from their server it gives me a 401 code.所以我正在尝试使用 Spotify API 构建一个随机播放列表生成器,当我从他们的服务器获取信息时,它给了我一个 401 代码。 I followed a tutorial on how to get the access token and now I have it.我遵循了有关如何获取访问令牌的教程,现在我拥有了。

My question is how do I use this token now?我的问题是我现在如何使用这个令牌? I've gotten the 401 error again but I think it's because I don't know how to order the url?我又收到了 401 错误,但我认为这是因为我不知道如何订购 url?

JS/html: 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>

I'm still a bit of a newbie at js/ajax (this is my first API project) and I've been following a tutorial where at the time they didn't have to deal with the authorization.我仍然是 js/ajax 的新手(这是我的第一个 API 项目),我一直在关注一个教程,当时他们不必处理授权。 Any help or resources appreciated.任何帮助或资源表示赞赏。 Thanks.谢谢。

The access token must be sent in the headers:访问令牌必须在标头中发送:

curl -X GET " https://api.spotify.com/v1/search?q=Muse&type=track,artist&market=US " -H "Accept: application/json" -H "Authorization: Bearer myToken" 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'
    }
});

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

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