spotifyapi:error 401,“未提供令牌”

gywdnpxw  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(328)

我正试着按照youtube上的教程来习惯spotifyapi,但我还是不断地遇到同样的错误,尽管我检查了文档,如果我输入了一个错别字。我将我的代码与视频中共享的代码进行了比较,它们看起来完全相同,但我不断发现以下错误:

{
 "error": {
 "status": 401,
 "message": "No token provided"
 }
}

作为参考,我的js在这里

const app = {};
app.apiUrl = 'https://api.spotify.com/v1';

//Allow 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));

        $.when(...search)
            .then((...results) => {
                console.log(results);
            });
    });
};

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

//Using the IDs, get the albums

//Get tracks

//Build playlist

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

$(app.init);

我知道这段视频是4年前发布的,但我也检查了端点的文档,从4年前开始似乎没有任何变化。
进一步参考,我的html代码:

<body>
    <main class="main-container">
        <section>
            <div class="form">
                <img src="images/note.svg" alt="">
                <form action="">
                    <input type="search" value="muse,ghost">
                    <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>
3okqufwl

3okqufwl1#

看起来您缺少api密钥或未正确传递它。

bweufnob

bweufnob2#

您已经定义了spotify访问令牌吗?
https://developer.spotify.com/

app.searchArtist = (artistName) => $.ajax({
    url: `${app.apiUrl}/search`,
    method:'GET',
    dataType: 'json',
    data: {
        q: artistName,
        type: 'artist'
    },
    headers: {
      "Authorization": `Bearer ${yourToken}`
    }
});

相关问题