You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Spotify API修改播放列表封面返回404错误求助

Spotify API 修改播放列表封面返回404问题

我在连续调用Spotify API完成创建播放列表、添加曲目、修改封面的流程时,createPlaylist()和populatePlaylist()都能正常工作,但changePicture()始终返回404错误。已经确认权限范围里加了ugc-image-upload,而且只有用静态播放列表ID替换playlistInfo.id时,修改封面的函数才能正常运行。

主函数

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

    // 确认ID存在
    console.log("Playlist created with ID:", playlistInfo.id);

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

    // 只有用静态ID替换这里的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;
}

问题排查与解决方法

1. 解决播放列表同步延迟问题

刚创建的播放列表在Spotify后端需要一定时间完成数据同步,直接调用修改封面接口会因资源未完全生成导致404。可以在调用changePicture前添加短暂延迟:

// 在populatePlaylist之后、changePicture之前加入延迟
await new Promise(resolve => setTimeout(resolve, 1500)); // 延迟1.5秒,可根据实际情况调整

2. 验证playlistInfo.id的正确性

打印完整的playlistInfo对象,确认返回的ID是纯字符串,没有多余字符或spotify:playlist:前缀(Spotify API只需要纯ID)。如果ID格式不对,会导致接口找不到资源。

3. 确认Token权限范围

调用GET https://api.spotify.com/v1/me接口,检查返回的scope字段是否包含ugc-image-upload。有时即使申请了权限,Token可能未正确携带该权限,需要重新生成Token。

4. 检查Base64图片格式

Spotify要求封面图片是JPEG格式,且Base64字符串不能包含data:image/jpeg;base64,前缀,只需要纯Base64内容。检查convertBase64函数的返回值,确保符合要求:

// 示例:移除Base64前缀
base64Image = base64Image.replace(/^data:image\/jpeg;base64,/, '');

内容的提问来源于stack exchange,提问作者Goldquick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 04:56:34