为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

江户川乱折腾 2023-10-14 16:46:00
尽管我检查了文档(如果我犯了拼写错误),但我仍然遇到相同的错误。我将我的代码与视频中共享的代码进行了比较,它们看起来完全相同,但我不断收到此错误:{ "error": { "status": 401, "message": "No token provided" }}作为参考,我的js在这里const app = {};app.apiUrl = 'https://api.spotify.com/v1';//Allow user to enter some namesapp.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 nameapp.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 playlistapp.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>
查看完整描述

3 回答

?
慕斯709654

TA贡献1840条经验 获得超5个赞

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

https://img1.sycdn.imooc.com/652a55950001e36924851286.jpg


查看完整回答
反对 回复 2023-10-14
?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

您是否已经定义了 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}`

    }

});


查看完整回答
反对 回复 2023-10-14
?
大话西游666

TA贡献1817条经验 获得超14个赞

您似乎丢失了 API 密钥或未正确传递它。



查看完整回答
反对 回复 2023-10-14
  • 3 回答
  • 0 关注
  • 230 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号