I'm using the v3 Google YouTubeAPI to get all videos from a channel, but of the 3 available, only one is returned. Any idea what is going wrong? Here is the code for the function:
public List<Video> GetVideos()
{
var vids = new List<Video>();
YouTubeService youtube = new YouTubeService(new BaseClientService.Initializer());
SearchResource.ListRequest listRequest = youtube.Search.List("id,snippet");
listRequest.Key = WebConfigurationManager.AppSettings["youTubeKey"];
listRequest.ChannelId = WebConfigurationManager.AppSettings["youTubeChannel"];
listRequest.MaxResults = 25;
listRequest.Type = "video";
SearchListResponse resp = listRequest.Execute();
foreach (SearchResult result in resp.Items)
{
vids.Add(new Video(result.Id.VideoId, result.Snippet));
}
return vids;
}
I did verify that all 3 videos are public, and do play on YouTube.
Related
I'm working on project that uses the YouTube api v3 with the Google .net client libary.
It's done, however sometimes it takes long time to respond when getting a video-list from playlist id. When this happens, it gives error "bad request" after 1 minute and tells me request timed out.
Is there any way to increase the request timeout or another solution for this problem?
List<YTVideo> videos = new List<YTVideo>();
var searchRequest = youtubeService.PlaylistItems.List("snippet");
searchRequest.PlaylistId = playlistId;
searchRequest.MaxResults = 1;
var searchResponse = await searchRequest.ExecuteAsync();
var playlistItemsInfo = searchResponse.Items.FirstOrDefault();
// to get videos from playlistItemsInfo
foreach (var searchResult in searchResponse.Items)
{
var videoSearchRequest = youtubeService.Videos.List("snippet, statistics, contentDetails");
videoSearchRequest.Id = searchResult.Snippet.ResourceId.VideoId;
videoSearchRequest.MaxResults = 1;
var videoSearchResponse = videoSearchRequest.Execute();
var video = videoSearchResponse.Items.FirstOrDefault();
if (video != null)
{
YTVideo yTVideo = new YTVideo
{
Title = video.Snippet.Title,
VideoId = video.Id,
Image = video.Snippet.Thumbnails.Maxres != null ? video.Snippet.Thumbnails.Maxres.Url : video.Snippet.Thumbnails.High.Url,
IsSelected = true
};
videos.Add(yTVideo);
}
}
My problem happens in searchResponse when I execute search the request.
When I try to list albums with the PhotosLibraryService API, I always return null, has anyone been able to use the new Google Photos API yet? The picasa API no longer works.
public void ListAlbums()
{
var json = File.ReadAllText(_diretorio + "/PhotoClient/" + "client.json");
var cr = JsonConvert.DeserializeObject<PersonalServiceAccountCred>(json);
var xCred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.client_email)
{
Scopes = this._scopes
}.FromPrivateKey(cr.private_key));
// Create the service.
var service = new PhotosLibraryService(new Google.Apis.Services.BaseClientService.Initializer()
{
HttpClientInitializer = xCred
});
var albums = service.Albums.List().Execute();
}
Thanks!
I am having problems loading a playlist of videos from Youtube. I follow this guide but cant figure out what is wrong because I don't get an error.
var YouTubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "MyAPIID"});
var ChannelListRequest = YouTubeService.Channels.List("contentDetails");
ChannelListRequest.ForUsername = "YoutubeUser";
var ListResponse = ChannelListRequest.Execute();
foreach (var channel in ListResponse.Items) //No content in ListResponse.Items
When I execute the request it returns a empty response. The API Id is correct because it becomes error if I use a old one. I have tried with the username and id from the channel but none worked. What am I missing?
Alright I tried some things and I managed to retrieve my playlists of my channel like so:
var service = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "yourapikey",
ApplicationName = this.GetType().Name
});
var playListRequest = service.Playlists.List("snippet");
playListRequest.ChannelId = "yourchannelid";
var result = await playListRequest.ExecuteAsync();
With the playlist Id's you get from this response you can retrieve the video's like so:
var playListItemsRequest = service.PlaylistItems.List("snippet");
playListItemsRequest.PlaylistId = "yourplaylistid";
var result = await playListItemsRequest.ExecuteAsync();
I tried to get the the length of a Video i searched before, with the youtube v3 API you can download at nuget.org.
I know there are a lot of solution, but they are always written in php.
That is the code I am using Right now:
var searchListRequest = youtubeService.Search.List("snippet");
earchListRequest.Q = Find;
searchListRequest.MaxResults = 5;
var searchListResponse = await searchListRequest.ExecuteAsync();
foreach (var searchResult in searchListResponse.Items)
{
switch (searchResult.Id.Kind)
{
case "youtube#video":
break;
}
}
Thanks for any kind of help :)
You can donwlaod the Json version of the video :
WebClient myDownloader = new WebClient();
myDownloader.Encoding = System.Text.Encoding.UTF8;
string jsonResponse=myDownloader.DownloadString(
"https://www.googleapis.com/youtube/v3/videos?id=" + yourvideoID+ "&key="
+ youtubekey + "&part=contentDetails");
dynamic dynamicObject = Json.Decode(jsonResponse);
string tmp = dynamicObject.items[0].contentDetails.duration;
var Duration = Convert.ToInt32
(System.Xml.XmlConvert.ToTimeSpan(tmp).TotalSeconds);
https://www.youtube.com/channel/UC-9-kyTW8ZkZNDHQJ6FgpwQ
public async System.Threading.Tasks.Task<List<ViewModel.YoutubeVideo>> GetYoutubeMusic()
{
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "....",
ApplicationName = this.GetType().ToString()
});
var searchListRequest = youtubeService.Channels.List("snippet");
searchListRequest.Id= "UC-9-kyTW8ZkZNDHQJ6FgpwQ";
searchListRequest.MaxResults = 25;
//Call the search.list method to retrieve results...
var searchListResponse = await searchListRequest.ExecuteAsync();
List<ViewModel.YoutubeVideo> arrays = new List<ViewModel.YoutubeVideo>();
foreach (var searchResult in searchListResponse.Items)
{
product = new ViewModel.YoutubeVideo();
product.id = searchResult.Id;
product.Name = searchResult.Snippet.Title;
product.Thumb100Uri = searchResult.Snippet.Thumbnails.Default.Url;
product.Thumb200Uri = searchResult.Snippet.Thumbnails.Medium.Url;
arrays.Add(product);
}
return arrays;
}
just be get information from this channel but no video...
I don't understand about that. Please to solve it.
Because you are not calling the search.list method. You are calling the channels.list method in your code.
But if you already have the id of the channel, you should just retrieve that channel's uploaded videos.