Spotify api PUT /playlists/{playlist_id}/images 返回具有有效 ID 的 404

问题描述 投票:0回答:1

我正在使用 Spotify 的 api 快速连续地创建播放列表、添加曲目并更改自定义播放列表封面图像。即使快速连续,

createPlaylist()
populatePlaylist()
功能也能完美运行。但是
changePicture()
函数返回 404 错误。

需要明确的是,是的,我确实在我的范围中添加了“ugc-image-upload”。

主要功能

export const savePlaylist = async (token: string, spotifyUserId: string, body: any) => {
    const playlistInfo = await createPlaylist(token, spotifyUserId, body);

    // CHECKING IF THE ID TRULY EXISTS
    console.log("Playlist created with ID:", playlistInfo.id);

    await populatePlaylist(token, playlistInfo.id, body.tracks);

    // THIS ONLY WORKS WHEN USING A STATIC ID INSTEAD OF PLAYLISTINFO.ID
    const changePictureResult = await changePicture(playlistInfo.id, token);
    if (!changePictureResult) {
        console.error("Failed to change playlist picture.");
    }

    console.log("Playlist picture changed.");

    return playlistInfo;
}

创建播放列表功能

const createPlaylist = async(token: string, userId:string, body:any) => {
    let url = `https://api.spotify.com/v1/users/${userId}/playlists`
    let axiosbody = {
        "name": body.title || "Custom Mix for you",
        "description": body.description || `This playlist was created specifically for you.`,
        "public": body.public || false,
        "collaborative": false
    }

    let result = await axios.post(url, axiosbody, {
        headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${token}`
        }
    }).catch(error => console.error(error));

    if(!result) return null;
    return result.data;
}

更改图片功能

const changePicture = async (playlistId:string, token:string) => {
    let base64Image = await convertBase64("Playlistcover_1.jpg")
    let url = `https://api.spotify.com/v1/playlists/${playlistId}/images`;
    let result = await axios.put(url, base64Image, {
        headers: {
            "Content-Type": "image/jpeg",
            "Authorization": `Bearer ${token}`
        }
    }).catch(error => console.error(error))

    if(!result) return false;
    return result.data;
}
javascript axios spotify
1个回答
0
投票

请在播放列表创建/填充和图像更新调用之间引入短暂的延迟(几秒钟)。这有时可以帮助缓解与传播延迟相关的问题。

await new Promise(resolve => setTimeout(resolve, 5000));
© www.soinside.com 2019 - 2024. All rights reserved.