Nit*_*esh 3 c# asp.net youtube-api youtube-channels youtube-data-api
我有一个ASP.Net网页,使用V2在我的频道中显示Youtube视频。由于Google已经淘汰了V2 API,因此我尝试使用V3 API,但无法从频道中获取视频。
我确实看过github上的示例,但是该示例显示了如何创建视频,但没有办法检索视频。在SO上搜索,我看到了使用php库的示例,我正在寻找特定于C#的东西。
有人可以帮我吗?
通过将频道ID添加到Search.list, 它会返回该频道中的视频列表。
var searchListRequest = service.Search.List("snippet");
searchListRequest.ChannelId = "UCIiJ33El2EakaXBzvelc2bQ";
var searchListResult = searchListRequest.Execute();
Run Code Online (Sandbox Code Playgroud)
更新对评论的响应,以了解发生的情况:
实际上,搜索将返回与通道ID相关联的所有内容,毕竟您是在搜索通道ID。
搜索返回一个SearchListResponse,其中包含许多项目。每个项目的类型为SearchResource搜索资源可以具有不同的类型或种类。在下面的两张图片中,您可以看到第一个是友善youtube#channel
的,第二个是友善的youtube#video
,您可以通过它们循环查找youtube视频。如果滚动到search.list页面的底部,则可以尝试一下并查看API返回的原始JSon。
解:
现在,如果您只想找回视频,只需在请求中添加类型即可告诉您所有视频就是:
searchListRequest.Type = "video";
Run Code Online (Sandbox Code Playgroud)
尽管前一段时间被问到我也一直在搜索如何使用 C# 从频道获取视频(全部)。目前我有支持分页的方法(可能可以做得更好:))
希望这可以帮助
public Task<List<SearchResult>> GetVideosFromChannelAsync(string ytChannelId)
{
return Task.Run(() =>
{
List<SearchResult> res = new List<SearchResult>();
var _youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "AIzaXyBa0HT1K81LpprSpWvxa70thZ6Bx4KD666",
ApplicationName = "Videopedia"//this.GetType().ToString()
});
string nextpagetoken = " ";
while (nextpagetoken != null)
{
var searchListRequest = _youtubeService.Search.List("snippet");
searchListRequest.MaxResults = 50;
searchListRequest.ChannelId = ytChannelId;
searchListRequest.PageToken = nextpagetoken;
// Call the search.list method to retrieve results matching the specified query term.
var searchListResponse = searchListRequest.Execute();
// Process the video responses
res.AddRange(searchListResponse.Items);
nextpagetoken = searchListResponse.NextPageToken;
}
return res;
});
}
Run Code Online (Sandbox Code Playgroud)