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

使用JavaScript调用YouTube API时遭遇HTTP 400错误求助

YouTube API 获取频道视频时HTTP 400错误修复

问题描述

尝试通过YouTube API获取指定频道的视频,最初10分钟代码可正常运行,现在出现HTTP 400错误。使用的JavaScript代码如下:

let fetchUrl = `https://www.googleapis.com/youtube/v3/search?key=${API_KEY}&channelId=${channelId}`;
fetch(fetchUrl)
.then(response => response.json())
.then(resJson => console.log(resJson.items))

错误原因与修复方案

HTTP 400属于请求参数错误,YouTube Data API的search接口有必填参数未指定,这是核心问题:

  • part参数:所有YouTube Data API接口都要求该参数,用于指定返回数据包含的资源片段(比如snippet包含视频标题、缩略图等基础信息)。
  • 可选但建议添加**type=video**:过滤掉频道、播放列表等非视频结果,避免后续处理冗余数据。

修复后的代码

// 补充必填的part参数,添加type过滤视频
let fetchUrl = `https://www.googleapis.com/youtube/v3/search?key=${API_KEY}&channelId=${channelId}&part=snippet&type=video`;
fetch(fetchUrl)
.then(response => {
  // 新增状态检查,方便排查其他异常
  if (!response.ok) throw new Error(`请求失败:状态码 ${response.status}`);
  return response.json();
})
.then(resJson => {
  console.log(resJson.items);
  // 此处可遍历items处理视频数据
})
.catch(error => console.error('请求出错:', error));

额外排查点

  • 检查API_KEY是否耗尽每日配额:YouTube API有免费配额限制,配额用完会触发错误,可在Google Cloud控制台查看配额使用情况。
  • 确认channelId有效性:无效的频道ID也会导致400错误,可通过频道页面URL或YouTube Studio获取正确ID。

内容的提问来源于stack exchange,提问作者Görkem

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 15:35:43