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

使用Spotify API创建播放列表Java报错400:JSON解析错误

解决Spotify API创建播放列表时的JSON解析400错误

嘿,我帮你捋捋这个问题——出现Error parsing JSON的400状态码,基本都是请求体或者请求头不符合Spotify API的要求导致的,我给你拆解几个关键修复点:

1. 先把请求头的Content-Type配置好

Spotify API认死了POST请求的Content-Type必须是application/json,要是没加这个头,服务器根本不知道你发的是JSON,自然会解析失败。你得给HttpPost加上这个配置:

post.setHeader("Content-Type", "application/json");
// 还有别忘了带Authorization头,格式是Bearer + 你的access token
post.setHeader("Authorization", "Bearer " + 你的AccessToken);

2. 检查请求体的JSON格式是否合规

创建播放列表至少得传name字段,可选的public、description也得是合法的JSON结构。千万别手动拼字符串(容易出错),用JSON库(比如Jackson或者Gson)生成更靠谱。举个Jackson的例子:

// 用Map构建播放列表参数
Map<String, Object> playlistParams = new HashMap<>();
playlistParams.put("name", "我的专属API播放列表");
playlistParams.put("public", false); // 私有播放列表
playlistParams.put("description", "用Java调用Spotify API创建的");

// 转成标准JSON字符串
ObjectMapper mapper = new ObjectMapper();
String jsonBody = mapper.writeValueAsString(playlistParams);

// 把JSON设置到请求体里
StringEntity entity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON);
post.setEntity(entity);

要是你非要手动拼JSON,一定要注意转义双引号,比如"{\"name\":\"My Playlist\"}",但真心不推荐,容易写错。

3. 确认userId和权限没问题

虽然你说已经加了所有权限,但还是再核对下:要创建公开播放列表需要playlist-modify-public权限,私有则需要playlist-modify-private,确保你的access token包含对应的权限。另外userId也要确认是正确的,别填错了账号ID。

补全后的完整代码片段

给你整理了一个能跑的完整示例,你可以参考着调整:

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;

public class SpotifyPlaylistCreator {
    public static void main(String[] args) throws Exception {
        String userId = "你的用户ID";
        String accessToken = "你的有效AccessToken";
        String apiUrl = "https://api.spotify.com/v1/users/" + userId + "/playlists";

        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpPost postRequest = new HttpPost(apiUrl);
            
            // 设置核心请求头
            postRequest.setHeader("Authorization", "Bearer " + accessToken);
            postRequest.setHeader("Content-Type", "application/json");
            
            // 构建合法的请求体
            Map<String, Object> playlistData = new HashMap<>();
            playlistData.put("name", "Java API创建的播放列表");
            playlistData.put("public", false);
            playlistData.put("description", "测试Spotify API的播放列表");
            
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonBody = objectMapper.writeValueAsString(playlistData);
            StringEntity requestEntity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON);
            postRequest.setEntity(requestEntity);
            
            // 发送请求并打印响应
            try (CloseableHttpResponse response = client.execute(postRequest)) {
                HttpEntity responseEntity = response.getEntity();
                String responseContent = EntityUtils.toString(responseEntity);
                System.out.println("响应状态: " + response.getStatusLine());
                System.out.println("响应内容: " + responseContent);
            }
        }
    }
}

额外小提醒

  • 要是还是报错,可以把你生成的JSON打印出来,检查下格式是否合法。
  • 也确认下access token有没有过期,过期的token有时候也会触发奇怪的错误提示哦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:16:55