Spotify API 错误 401 - 尝试将曲目放入用户的库中

Posted

技术标签:

【中文标题】Spotify API 错误 401 - 尝试将曲目放入用户的库中【英文标题】:Spotify API Error 401 - Attempting to PUT tracks into a user's library 【发布时间】:2020-06-27 00:55:17 【问题描述】:

只是想知道是否有人能在此代码中发现会引发 401 未授权错误的问题?

我有几个类似的功能,所有功能都很好,通过 Spotify Web API 写入 GET 数据(使用 axios),但由于某种原因,我的 PUT 请求引发了错误。

这是此端点的relevant Spotify documentation。

基本上,数据作为 JSON 对象数组从客户端发送到服务器,其中包含我想保存到授权 Spotify 帐户的曲目的详细信息,以及作为参数的有效访问令牌。我知道令牌是有效的,因为在此步骤之前我正在通过不同的 GET 获取用户的帐户数据。而且我知道该 url 已正确连接,因为它已在错误详细信息中打印出来:

https://api.spotify.com/v1/me/tracks?ids=5xDyeaAwdQROKqXxkGeMin%2C1Hli68AYjHPUvtF9bkr0Pe%2C2lId1EsB3LrE8rGYSBuUCy%2C0GuaEFOi9JIggdVpJulBRf%2C1ikymkBwiyQpTshPiddgRC%2C7aboHXhiWEtwft9TMAFWoK%2C3ipalmig9WXTT8KTy17OJ0%2C1twjROI1HSU0LMgP391g5l%2C0iKqQUmVOy2PEMGVhT6jBj%2C3ImgAYVk1pOeih6iFaAa0N%2C0pRzr7f2z9zHr8oqJ4Hchx%2C2racu4ScxU1gVtdpvRfUP9%2C3uoR6wF45u1W3uqAQpswg4%2C564LOaf2QQQtz2faWgWFys%2C0sO5cHkWVlwD9gv5aq1OjV%2C3WfRxX5uM7RfgqLnzftK0g%2C3yTMJDbmXKfZi0OW2g4193%2C0CdaMDNIfgQu0wR5jnXnb3%2C48ovG2U4PQPHVEosDR7lrs%2C1arM2HxLWtsCSImxVgYAzf%2C6gOl5JmXB8vUrMHo2BeeKb%2C53fBgckGdEXTQ2YhvdlGjZ%2C3tioypT6P9clCNTmThWqBL%2C2CPupwTdC35SCXCbALD7MY%2C7dKeVDydH4yqEzVNx6hui9%2C2rpJtmeLU52N5WbR7BLfUX%2C0PrZsr6FIiBu3p8uJrbFjg%2C57yUjPDCY10YUcXLhUFY4F%2C3eEe37Y0ecng4dD8awUnA7%2C0IAF0KIM3Cnp7WhOVJfFuS%2C600rxqKGJUS6gMD5klZj5l%2C24ZZPWkt2ELVI26tpiZZ1K%2C69shxYcOeyQiMJDIVlHZGE%2C6CoD3wMYARMmkjaaxiWJmZ%2C1TpTmnDAIMxvH7xIFx0zxG%2C33eI5sAdWvSGrAMW7DzWIB%2C6W9lfjN0fuvGW4nEqR9WKe%2C6kxVzbsyvnckVOIzrXrXMR%2C3I32EQp0LdVuQixucRuzq0%2C5cBR3cFJReCbjSBOarnysu%2C7zfgnI2IzmkNhQWkg1LK0u%2C5TuQeDUDFHWRFAOp8dEsn0%2C1qbcfwtK2ZhaKPnkuNdDvQ%2C5IHSXDJh9vwY9JYK39CPUx%2C3QKMflLzzaURXEcxqDXS2q%2C46Pz8LnBjGFdu19VyCPeRn%2C5rYbEHPNqqFeGQGvaw5Z0k%2C00ULB3uUHt2sR2NVkhbr59%2C0NWr0JWZmAM9A2U1U5cAn4%2C6bhWSSAQjThmnJqp2aFnaW

非常感谢任何帮助!

app.put("/migration-data/put/:token", async (req, res) => 
    console.log(`Migrating data to new account using token $req.params.token.substring(0, 4)...\n`);
    const toClientPutData = await putData(req.params.token, req.body);
    res.json(toClientPutData);
);

const putData = async (token, data) => 
    const putResults = ;

    const spotifyAPI_PutTracks = async (trackData, index=0) => 
        try            
            const recurse = ((trackData.length - 1) - (index + 50)) > 0;

            const offset = recurse ? 49 : (trackData.length - index) - 1;
            var tracksThisQuery = "";

            for (var i = index; i < (index + offset); i++) 
                tracksThisQuery+=`$trackData[i].id%2C`
            

            tracksThisQuery+=`$trackData[(index + offset)].id`

            const puttingTracks = await axios.put(
                `https://api.spotify.com/v1/me/tracks?ids=$tracksThisQuery`,
                 headers:  Authorization: `Bearer $token`  
            );

            if (recurse) 
                return spotifyAPI_PutTracks(trackData, index+=50);
            

            return puttingTracks.data;

         catch (apiError) 
            console.log(apiError);
        
    

    const  user, savedTracks, savedAlbums, following, playlists  = data;

    if (token) 
        // put tracks
        const putTracksData = await spotifyAPI_PutTracks(savedTracks);
        putResults["putTracks"] = putTracksData;
    

    return putResults;

【问题讨论】:

【参考方案1】:

想通了 - PUT 需要在 url 之后,但在 headers 之前有一个额外的 data 参数。我刚刚在第二个参数槽中插入了一个空对象字面量,然后我们就开始比赛了。

【讨论】:

以上是关于Spotify API 错误 401 - 尝试将曲目放入用户的库中的主要内容,如果未能解决你的问题,请参考以下文章

向 Spotify API 发出 PUT 请求时出现 401 错误

请求从 Spotify Web API 获取用户信息导致 401 错误

我在 c# wpf 中使用 spotify api 收到错误 401 未授权

Spotify Web API - 缺少 401 权限

Axios GET 请求 Spotify API 返回 401

如何使用我的 spotify api 令牌?