如何通过YouTube VideoId API调用获取视频标签?
解决YouTube VideoId API获取标签返回null的问题
1. 确认API请求的Part参数
调用YouTube Data API的videos.list接口时,必须在part参数中包含snippet(你已经拿到了snippet的其他字段,这一步大概率没问题,但仍需确认请求代码里的part是否设置为包含snippet的组合,比如snippet,contentDetails,statistics)。
2. 视频本身无公开标签
你提供的API响应里,snippet字段确实没有tags节点,说明该视频的作者未设置公开标签,或者标签未对外展示。这种情况下,snippet.getTags()返回null是正常的——因为API本身就没有返回对应数据。
3. 提取描述中的Hashtag作为替代
如果需要类似标签的内容,可以从视频描述文本里提取hashtag(比如你示例里的#Solomon #Proverbs #Wisdom),用正则匹配即可。Java示例代码:
public static void printVideo(int index, @NonNull Video video) { VideoSnippet snippet = video.getSnippet(); log.trace("Video[" + index + "] Tags = {}", snippet.getTags()); // 提取描述中的hashtag String description = snippet.getDescription(); if (description != null) { Pattern hashtagPattern = Pattern.compile("#(\\w+)"); Matcher matcher = hashtagPattern.matcher(description); List<String> hashtags = new ArrayList<>(); while (matcher.find()) { hashtags.add(matcher.group(1)); } log.trace("Video[" + index + "] Hashtags from description = {}", hashtags); } }
内容的提问来源于stack exchange,提问作者selvi
相关产品推荐
相关产品推荐

