如何通过Channel ID获取YouTube未列出视频(C# SDK/HTTP请求)
获取YouTube频道所有视频(含未列出内容)的解决方案(所有者身份)
嘿,作为频道所有者,你完全有办法拿到自己频道里的未列出视频——核心点就是必须用OAuth 2.0认证,普通的API密钥只能拉公开内容,根本碰不到未列出/私有视频。下面给你两种方案,优先用官方C# SDK,也给了HTTP请求的路子:
一、C# 官方SDK方案(最省心)
用Google的Google.Apis.YouTube.v3 NuGet包,官方封装好了所有细节,不用自己拼HTTP请求。
1. 先装NuGet包
打开Package Manager Console,跑这俩命令:
Install-Package Google.Apis.YouTube.v3 Install-Package Google.Apis.Auth.OAuth2
2. 代码实现(带认证+拉取全量视频)
首先你得去Google Cloud Console创建一个"桌面应用"类型的OAuth客户端ID,下载JSON凭据文件(命名比如client_secret.json),然后用下面的代码:
using Google.Apis.Auth.OAuth2; using Google.Apis.Services; using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3.Data; using System; using System.IO; using System.Threading; class YouTubeChannelVideoGetter { // 要获取私有/未列出视频,必须要这个权限 private static readonly string[] RequiredScopes = { YouTubeService.Scope.YoutubeReadonly }; private const string AppName = "My Channel Video Fetcher"; static void Main(string[] args) { UserCredential credential; // 加载你的OAuth凭据文件 using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) { credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, RequiredScopes, "user", CancellationToken.None).Result; } // 初始化YouTube服务 var youtubeService = new YouTubeService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = AppName, }); // 构建搜索请求:指定频道ID,只拉视频 var searchRequest = youtubeService.Search.List("id,snippet"); searchRequest.ChannelId = "YOUR_UC_CHANNEL_ID"; // 替换成你的频道UC开头ID searchRequest.MaxResults = 50; // 单次最多50条,分页拿剩下的 searchRequest.Type = "video"; // 执行请求,处理第一页结果 var response = searchRequest.Execute(); PrintVideos(response.Items); // 处理分页,拿到所有视频 while (!string.IsNullOrEmpty(response.NextPageToken)) { searchRequest.PageToken = response.NextPageToken; response = searchRequest.Execute(); PrintVideos(response.Items); } } private static void PrintVideos(IList<SearchResult> videos) { foreach (var video in videos) { Console.WriteLine($"标题: {video.Snippet.Title}"); Console.WriteLine($"视频ID: {video.Id.VideoId}"); Console.WriteLine($"可见性: {video.Snippet.PrivacyStatus}"); // 会显示public/unlisted/private Console.WriteLine("------------------------"); } } }
关键点提醒
- 权限:
YoutubeReadonly足够了,要是你还要修改视频,就用Youtube权限。 - 频道ID:一定要用
UCxxxx格式的ID,不是自定义URL里的名字。可以用channels.list接口,通过forUsername参数拿到自己的频道ID。 - 分页:API单次最多返回50条,所以必须用
NextPageToken循环拉取所有内容。
二、HTTP请求方案(不用SDK)
如果不想依赖SDK,直接调用YouTube Data API的search.list端点就行,同样得带OAuth令牌。
1. 先拿OAuth访问令牌
得走OAuth 2.0授权流程拿到access_token(具体流程你可以查Google OAuth 2.0的桌面应用授权步骤,这里假设你已经拿到有效的令牌了)。
2. 发送GET请求
请求URL结构:
GET https://www.googleapis.com/youtube/v3/search?part=id,snippet&channelId=YOUR_UC_ID&maxResults=50&type=video&access_token=YOUR_ACCESS_TOKEN
响应简化示例
{ "items": [ { "id": {"videoId": "xyz789"}, "snippet": { "title": "我的未列出视频", "privacyStatus": "unlisted" } } ], "nextPageToken": "abc123" }
注意事项
- 必须带
access_token,不然只能拿到公开视频。 - 分页的话,把
nextPageToken加到下一次请求的参数里就行。
最后几个重要提示
- 认证是刚需:不管用哪种方案,没有OAuth认证,你根本拿不到未列出/私有视频——哪怕你是频道主人也不行,API密钥只给看公开内容。
- API配额:
search.list每次请求耗100配额,每天默认10000配额,别一次性拉太多,不然会被限流。 - 私有视频:这段代码/请求也能拿到私有视频,因为你是所有者且通过OAuth授权了。
内容的提问来源于stack exchange,提问作者Petros
相关产品推荐
相关产品推荐

