youtube

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

YouTube Data API

YouTube Data API

Use the YouTube Data API v3 via direct
curl
calls to search videos, retrieve video details, get channel information, list playlist items, and fetch comments.
Official docs:
https://developers.google.com/youtube/v3

通过直接调用
curl
来使用YouTube Data API v3,可搜索视频、获取视频详情、查询频道信息、列出播放列表内容以及提取评论
官方文档:
https://developers.google.com/youtube/v3

When to Use

适用场景

Use this skill when you need to:
  • Search videos by keywords or filters
  • Get video details (title, description, statistics, duration)
  • Get channel info (subscriber count, video count, description)
  • List playlist items (videos in a playlist)
  • Fetch comments on videos
  • Get trending videos by region

在以下场景中可使用该技能:
  • 搜索视频:按关键词或筛选条件查找
  • 获取视频详情(标题、描述、统计数据、时长)
  • 查询频道信息(订阅数、视频数、频道描述)
  • 列出播放列表内容(播放列表中的视频)
  • 提取视频评论
  • 按地区获取热门视频

Prerequisites

前置条件

1. Create Google Cloud Project

1. 创建Google Cloud项目

  1. Go to Google Cloud Console
  2. Create a new project or select existing one
  3. Go to "APIs & Services" > "Library"
  4. Search for "YouTube Data API v3" and enable it
  1. 访问Google Cloud Console
  2. 创建新项目或选择现有项目
  3. 进入「API与服务」>「库」
  4. 搜索「YouTube Data API v3」并启用它

2. Get API Key

2. 获取API密钥

  1. Go to "APIs & Services" > "Credentials"
  2. Click "Create Credentials" > "API Key"
  3. Copy the API key
bash
export YOUTUBE_API_KEY="AIzaSy..."
  1. 进入「API与服务」>「凭据」
  2. 点击「创建凭据」>「API密钥」
  3. 复制API密钥
bash
export YOUTUBE_API_KEY="AIzaSy..."

3. (Optional) Restrict API Key

3. (可选)限制API密钥

For production use, restrict the key:
  • Application restrictions: HTTP referrers, IP addresses
  • API restrictions: YouTube Data API v3 only

Important: When using
$VAR
in a command that pipes to another command, wrap the command containing
$VAR
in
bash -c '...'
. Due to a Claude Code bug, environment variables are silently cleared when pipes are used directly.
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY" | jq .'
生产环境使用时,请限制密钥权限:
  • 应用限制:HTTP引用来源、IP地址
  • API限制:仅允许YouTube Data API v3

重要提示:当在包含管道的命令中使用
$VAR
时,请将包含
$VAR
的命令用
bash -c '...'
包裹。由于Claude Code的一个bug,直接使用管道时环境变量会被静默清除。
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY" | jq .'

How to Use

使用方法

Base URL:
https://www.googleapis.com/youtube/v3

基础URL:
https://www.googleapis.com/youtube/v3

1. Search Videos

1. 搜索视频

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&q=kubernetes+tutorial&type=video&maxResults=5&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {videoId: .id.videoId, title: .snippet.title, channel: .snippet.channelTitle}'

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&q=kubernetes+tutorial&type=video&maxResults=5&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {videoId: .id.videoId, title: .snippet.title, channel: .snippet.channelTitle}'

2. Search with Filters

2. 按筛选条件搜索

Search for videos uploaded this year, ordered by view count:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&q=react+hooks&type=video&order=viewCount&publishedAfter=2024-01-01T00:00:00Z&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {videoId: .id.videoId, title: .snippet.title}'

搜索今年上传的视频,按播放量排序:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&q=react+hooks&type=video&order=viewCount&publishedAfter=2024-01-01T00:00:00Z&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {videoId: .id.videoId, title: .snippet.title}'

3. Get Video Details

3. 获取视频详情

Replace
<your-video-id>
with an actual video ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics,contentDetails&id=<your-video-id>&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {title: .snippet.title, views: .statistics.viewCount, likes: .statistics.likeCount, duration: .contentDetails.duration}'

<your-video-id>
替换为实际视频ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics,contentDetails&id=<your-video-id>&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {title: .snippet.title, views: .statistics.viewCount, likes: .statistics.likeCount, duration: .contentDetails.duration}'

4. Get Multiple Videos

4. 获取多个视频信息

Replace
<your-video-id-1>
,
<your-video-id-2>
,
<your-video-id-3>
with actual video IDs:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id=<your-video-id-1>,<your-video-id-2>,<your-video-id-3>&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {id: .id, title: .snippet.title, views: .statistics.viewCount}'

<your-video-id-1>
<your-video-id-2>
<your-video-id-3>
替换为实际视频ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id=<your-video-id-1>,<your-video-id-2>,<your-video-id-3>&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {id: .id, title: .snippet.title, views: .statistics.viewCount}'

5. Get Trending Videos

5. 获取热门视频

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&chart=mostPopular&regionCode=US&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {title: .snippet.title, channel: .snippet.channelTitle, views: .statistics.viewCount}'

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&chart=mostPopular&regionCode=US&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {title: .snippet.title, channel: .snippet.channelTitle, views: .statistics.viewCount}'

6. Get Channel by ID

6. 通过ID获取频道信息

Replace
<your-channel-id>
with an actual channel ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&id=<your-channel-id>&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {title: .snippet.title, subscribers: .statistics.subscriberCount, videos: .statistics.videoCount}'

<your-channel-id>
替换为实际频道ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&id=<your-channel-id>&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {title: .snippet.title, subscribers: .statistics.subscriberCount, videos: .statistics.videoCount}'

7. Get Channel by Handle

7. 通过Handle获取频道信息

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&forHandle=@GoogleDevelopers&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {id: .id, title: .snippet.title, subscribers: .statistics.subscriberCount}'

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&forHandle=@GoogleDevelopers&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {id: .id, title: .snippet.title, subscribers: .statistics.subscriberCount}'

8. Get Channel by Username

8. 通过用户名获取频道信息

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&forUsername=GoogleDevelopers&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {id: .id, title: .snippet.title, description: .snippet.description}'

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&forUsername=GoogleDevelopers&key=${YOUTUBE_API_KEY}"' | jq '.items[0] | {id: .id, title: .snippet.title, description: .snippet.description}'

9. List Playlist Items

9. 列出播放列表内容

Replace
<your-playlist-id>
with an actual playlist ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=<your-playlist-id>&maxResults=20&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {position: .snippet.position, title: .snippet.title, videoId: .snippet.resourceId.videoId}'

<your-playlist-id>
替换为实际播放列表ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=<your-playlist-id>&maxResults=20&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {position: .snippet.position, title: .snippet.title, videoId: .snippet.resourceId.videoId}'

10. Get Channel Uploads Playlist

10. 获取频道上传播放列表

First get the channel's uploads playlist ID, then list videos. Replace
<your-channel-id>
with an actual channel ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=<your-channel-id>&key=${YOUTUBE_API_KEY}"' | jq -r '.items[0].contentDetails.relatedPlaylists.uploads'

先获取频道的上传播放列表ID,再列出视频。将
<your-channel-id>
替换为实际频道ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=<your-channel-id>&key=${YOUTUBE_API_KEY}"' | jq -r '.items[0].contentDetails.relatedPlaylists.uploads'

11. Get Video Comments

11. 获取视频评论

Replace
<your-video-id>
with an actual video ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=<your-video-id>&maxResults=20&order=relevance&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {author: .snippet.topLevelComment.snippet.authorDisplayName, text: .snippet.topLevelComment.snippet.textDisplay, likes: .snippet.topLevelComment.snippet.likeCount}'

<your-video-id>
替换为实际视频ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=<your-video-id>&maxResults=20&order=relevance&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {author: .snippet.topLevelComment.snippet.authorDisplayName, text: .snippet.topLevelComment.snippet.textDisplay, likes: .snippet.topLevelComment.snippet.likeCount}'

12. Search Comments

12. 搜索视频评论

Replace
<your-video-id>
with an actual video ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=<your-video-id>&searchTerms=great+video&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {author: .snippet.topLevelComment.snippet.authorDisplayName, text: .snippet.topLevelComment.snippet.textDisplay}'

<your-video-id>
替换为实际视频ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=<your-video-id>&searchTerms=great+video&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {author: .snippet.topLevelComment.snippet.authorDisplayName, text: .snippet.topLevelComment.snippet.textDisplay}'

13. Get Video Categories

13. 获取视频分类

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&regionCode=US&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {id: .id, title: .snippet.title}'

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&regionCode=US&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {id: .id, title: .snippet.title}'

14. Search Videos by Category

14. 按分类搜索视频

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&videoCategoryId=28&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {videoId: .id.videoId, title: .snippet.title}'
Note: Category 28 = Science & Technology

bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&videoCategoryId=28&maxResults=10&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {videoId: .id.videoId, title: .snippet.title}'
注:分类28 = 科学与技术

15. Get Playlists from Channel

15. 获取频道的播放列表

Replace
<your-channel-id>
with an actual channel ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=<your-channel-id>&maxResults=20&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {id: .id, title: .snippet.title, description: .snippet.description}'

<your-channel-id>
替换为实际频道ID:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=<your-channel-id>&maxResults=20&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {id: .id, title: .snippet.title, description: .snippet.description}'

Common Video Categories

常见视频分类

IDCategory
1Film & Animation
10Music
17Sports
20Gaming
22People & Blogs
24Entertainment
25News & Politics
26Howto & Style
27Education
28Science & Technology

IDCategory
1Film & Animation
10Music
17Sports
20Gaming
22People & Blogs
24Entertainment
25News & Politics
26Howto & Style
27Education
28Science & Technology

Part Parameter Options

Part参数选项

Videos

视频

  • snippet
    - Title, description, thumbnails, channel
  • statistics
    - Views, likes, comments count
  • contentDetails
    - Duration, definition, caption
  • status
    - Upload status, privacy, license
  • player
    - Embeddable player
  • snippet
    - 标题、描述、缩略图、频道信息
  • statistics
    - 播放量、点赞数、评论数
  • contentDetails
    - 时长、清晰度、字幕
  • status
    - 上传状态、隐私设置、授权协议
  • player
    - 可嵌入播放器

Channels

频道

  • snippet
    - Title, description, thumbnails
  • statistics
    - Subscribers, videos, views
  • contentDetails
    - Related playlists (uploads, likes)
  • brandingSettings
    - Channel customization

  • snippet
    - 标题、描述、缩略图
  • statistics
    - 订阅数、视频数、播放量
  • contentDetails
    - 关联播放列表(上传、点赞)
  • brandingSettings
    - 频道自定义设置

Pagination

分页处理

Use
nextPageToken
from response to get more results. Replace
<your-next-page-token>
with the actual token from the previous response:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&q=python&type=video&maxResults=50&pageToken=<your-next-page-token>&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {title: .snippet.title}'

使用响应中的
nextPageToken
获取更多结果。将
<your-next-page-token>
替换为上一次响应中的实际令牌:
bash
bash -c 'curl -s "https://www.googleapis.com/youtube/v3/search?part=snippet&q=python&type=video&maxResults=50&pageToken=<your-next-page-token>&key=${YOUTUBE_API_KEY}"' | jq '.items[] | {title: .snippet.title}'

Guidelines

注意事项

  1. Quota limits: API has 10,000 units/day quota. Search costs 100 units, most others cost 1 unit
  2. Rate limits: Implement exponential backoff on 403/429 errors
  3. API key security: Never expose API keys in client-side code
  4. Caching: Cache responses to reduce quota usage
  5. Video IDs: Extract from URLs like
    youtube.com/watch?v=VIDEO_ID
    or
    youtu.be/VIDEO_ID
  1. 配额限制:API每日配额为10000单位。搜索操作消耗100单位,其他大多数操作消耗1单位
  2. 速率限制:遇到403/429错误时,实现指数退避机制
  3. API密钥安全:切勿在客户端代码中暴露API密钥
  4. 缓存:缓存响应以减少配额消耗
  5. 视频ID提取:从
    youtube.com/watch?v=VIDEO_ID
    youtu.be/VIDEO_ID
    这类URL中提取视频ID