是否可以从一个频道(而不是我的)获取所有视频?如果可能,我可以使用简单的API密钥吗?还是应该使用OAuth 2.0凭据?
我用这种方式做了,它对我有效我使用了来自Nuget数据包管理器的Youtube API v3
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
public ActionResult GetVideo(YouTubeData objYouTubeData)
{
try
{
var yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "Your API Key" });
var channelsListRequest = yt.Channels.List("contentDetails");
channelsListRequest.ForUsername = "kkrofficial";
var channelsListResponse = channelsListRequest.Execute();
foreach (var channel in channelsListResponse.Items)
{
// of videos uploaded to the authenticated user's channel.
var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistItemsListRequest = yt.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;
// Retrieve the list of videos uploaded to the authenticated user's channel.
var playlistItemsListResponse = playlistItemsListRequest.Execute();
foreach (var playlistItem in playlistItemsListResponse.Items)
{
// Print information about each video.
//Console.WriteLine("Video Title= {0}, Video ID ={1}", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
var qry = (from s in ObjEdbContext.ObjTubeDatas where s.Title == playlistItem.Snippet.Title select s).FirstOrDefault();
if (qry == null)
{
objYouTubeData.VideoId = "https://www.youtube.com/embed/" + playlistItem.Snippet.ResourceId.VideoId;
objYouTubeData.Title = playlistItem.Snippet.Title;
objYouTubeData.Descriptions = playlistItem.Snippet.Description;
objYouTubeData.ImageUrl = playlistItem.Snippet.Thumbnails.High.Url;
objYouTubeData.IsValid = true;
ObjEdbContext.ObjTubeDatas.Add(objYouTubeData);
ObjEdbContext.SaveChanges();
ModelState.Clear();
}
}
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
}
catch (Exception e)
{
ViewBag.ErrorMessage = "Some exception occured" + e;
return RedirectToAction("GetYouTube");
}
return RedirectToAction("GetYouTube");
}
在这行中提供您的频道名称
channelsListRequest.ForUsername = "kkrofficial"; //kkrofficial is kkr channel name.
关注此链接https://developers.google.com/youtube/v3/code_samples/dotnet#retrieve_my_uploads
您可以使用您的API密钥,只需查询所有频道的视频(即使不是您的:)
public Task<List<SearchResult>> GetVideosFromChannelAsync(string ytChannelId)
{
return Task.Run(() =>
{
List<SearchResult> res = new List<SearchResult>();
string nextpagetoken = " ";
while (nextpagetoken != null)
{
var searchListRequest = _youtubeService.Search.List("snippet");
searchListRequest.MaxResults = 50;
searchListRequest.ChannelId = ytChannelId;
searchListRequest.PageToken = nextpagetoken;
searchListRequest.Type = "video";
// 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;
});
}
这个方法应该会让你进入的轨道
这里有一个关于如何使用控制台应用程序输出频道视频名称的快速示例。
您将需要一个API密钥。获取API密钥,
转到https://console.cloud.google.com/
使用您的gmail帐户登录。
单击启用API和服务(或者转到API和服务,如果您已经启用了它)。
在API库页面中,搜索youtube。
选择Youtube Data API v3。
单击启用以启用API。
单击左侧菜单上的凭据(或者单击管理,然后单击凭据(如果已启用))。
单击顶部的创建凭据。
选择API键。
复制API密钥值并粘贴到下面的代码中。
在Visual Studio中,创建一个新的.NET Framework控制台应用程序。转到Nuget数据包管理器。安装Google.Apis.Youtube.v3软件包。
using System;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
namespace GetYouTubeVideos
{
class Program
{
static void Main(string[] args)
{
GetVideos();
}
public static void GetVideos()
{
try
{
YouTubeService yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "put api key here" });
ChannelsResource.ListRequest channelsListRequest = yt.Channels.List("contentDetails");
channelsListRequest.ForUsername = "put channel user name here";
// channelsListRequest.Id = "put channel id here (if you want to use the channel id)";
ChannelListResponse channelsListResponse = channelsListRequest.Execute();
foreach (Channel channel in channelsListResponse.Items)
{
string uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
string nextPageToken = "";
while (nextPageToken != null)
{
PlaylistItemsResource.ListRequest playlistItemsListRequest = yt.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;
PlaylistItemListResponse playlistItemsListResponse = playlistItemsListRequest.Execute();
foreach (PlaylistItem playlistItem in playlistItemsListResponse.Items)
{
Console.WriteLine("Video Title= {0}, Video ID ={1}", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
}
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}