猿问

Spotify API:错误 401,“未提供令牌”

尽管我检查了文档(如果我犯了拼写错误),但我仍然遇到相同的错误。我将我的代码与视频中共享的代码进行了比较,它们看起来完全相同,但我不断收到此错误:


{

 "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>


江户川乱折腾
浏览 232回答 3
3回答

慕斯709654

转至https://developer.spotify.com/console/get-search-item/以生成 OAuth 令牌,并在 API 调用中使用相同的令牌作为授权承载令牌。

斯蒂芬大帝

您是否已经定义了 Spotify 访问令牌?https://developer.spotify.com/app.searchArtist = (artistName) => $.ajax({&nbsp; &nbsp; url: `${app.apiUrl}/search`,&nbsp; &nbsp; method:'GET',&nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; q: artistName,&nbsp; &nbsp; &nbsp; &nbsp; type: 'artist'&nbsp; &nbsp; },&nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; "Authorization": `Bearer ${yourToken}`&nbsp; &nbsp; }});

大话西游666

您似乎丢失了 API 密钥或未正确传递它。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答