Basically here is my scenario - I am trying to show videos from a youtube channel in an mvc application of mine. I have created the project in the api manager console, procured the key, enabled the Youtube Data API v3. Then I proceeded to write up my code for
fetching and displaying the list of videos. Although it seems that the requisite things have been done as per need and correctly, it is not so. Here's what I did -
1.
public class YouTubeData
{
public string Descriptions { get; set; }
public string Title { get; set; }
public string ImageUrl { get; set; }
public bool IsValid { get; set; }
public string VideoId { get; set; }
}
2.
public class VideoStocks
{
public static List<YouTubeData> GetVideoList()
{
List<YouTubeData> lstYtb = new List<YouTubeData>();
try
{
var yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "AIza66T1nNkMGy-1rdXZ9nkSOPPoO851a6i79o" });
var channelsListRequest = yt.Channels.List("contentDetails");
channelsListRequest.ForUsername = "UCIpaQzkTUSPsovR2lb1nFog";
var channelsListResponse = channelsListRequest.Execute(); <----- this is where trouble starts
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 = 8;
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)
{
YouTubeData objYouTubeData = new YouTubeData();
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;
lstYtb.Add(objYouTubeData);
// Print information about each video.
//Console.WriteLine("Video Title= {0}, Video ID ={1}", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
}
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
}
catch (Exception e)
{
string ErrorMessage = "Some exception occured" + e.Data.ToString()+e.Message.ToString()+e.GetBaseException().ToString();
}
return lstYtb;
}
}
3.
<ul id="vdoList" style="margin:11px">
@{
List<YouTubeData> lstYtb = VideoStocks.GetVideoList();
if(lstYtb.Count>0 && lstYtb!=null)
{
foreach (var item in lstYtb)
{
<li>
<div style="border-color:mediumaquamarine; height:290px; border-radius:8px; width:320px; min-width:320px">
<b>@item.Title</b>
<p style="color:rgb(38, 6, 6)">
@item.Descriptions
</p>
<br/>
<div>
@item.ImageUrl
</div>
</div>
</li>
}
}
else
{
<li>
<div style="border-color:mediumaquamarine; color:#2c302f; height:290px; border-radius:8px; width:320px; min-width:320px">
<p>
<i>
No videos available at this time. Check this section again at a later time for new additions!
</i>
</p>
</div>
</li>
}
}
</ul>
As you can see, the api key etc are there. Also mentioned the channel name in channelsListRequest.ForUsername = "UCIpaQzkTUSPsovR2lb1nFog"; If you go to the above username it's my channel whose title name is 'Protik PC'.
So upto this point things seemed as if ok. But then, trouble starts as var channelsListResponse = channelsListRequest.Execute();
returns nothing it is null. Why, I don't understand. All things I mentioned. So as of now, I am getting nothing, just that 'no videos at this time ....... .. ' message.
Can you folks spot something in what I wrote. Any small unmindful error, mistake or possible something else that I need to do, check, configure from the google project console end which may be required in order to make this work okay. What additional things
I need to code, or configure for this.
Kindly tell me please.
I have some more customization to do on this scenario but first things first I want to fetch and show them before doing anything else.
Thanks a lot in anticipation.
I have a colourful, happy life outside of coding. It encompasses many things. I welcome you, https://www.twitter.com/pro_indigo
There are more wonders in this world of ours than you can wonder; and it is nice to sometimes wonder at them.
Thanks to both of you for replying.
I've changed the code some as per jimmy69 's suggestion.
I'm now using
var srchListRqst = yt.Search.List("snippet"); srchListRqst.ChannelId = "UCIpaQzkTUSPsovR2lb1nFog";
This does work now. As it appear, since I had not used any custom username for my channel (in meaningful name format) it was only my channelID. And not username.
Getting the results nicely now, very satisfied with this.
Thanks to both!
I have a colourful, happy life outside of coding. It encompasses many things. I welcome you, https://www.twitter.com/pro_indigo
There are more wonders in this world of ours than you can wonder; and it is nice to sometimes wonder at them.
Member
44 Points
249 Posts
Youtube Video Data API and MVC Razor Coding checkpoint ---
Feb 01, 2017 04:37 PM|PGChoudhury|LINK
Hello to All.
Basically here is my scenario - I am trying to show videos from a youtube channel in an mvc application of mine. I have created the project in the api manager console, procured the key, enabled the Youtube Data API v3. Then I proceeded to write up my code for fetching and displaying the list of videos. Although it seems that the requisite things have been done as per need and correctly, it is not so. Here's what I did -
1.
2.
3.
As you can see, the api key etc are there. Also mentioned the channel name in
channelsListRequest.ForUsername = "UCIpaQzkTUSPsovR2lb1nFog";
If you go to the above username it's my channel whose title name is 'Protik PC'.
So upto this point things seemed as if ok. But then, trouble starts as
var channelsListResponse = channelsListRequest.Execute();
returns nothing it is null. Why, I don't understand. All things I mentioned. So as of now, I am getting nothing, just that 'no videos at this time ....... .. ' message.

Can you folks spot something in what I wrote. Any small unmindful error, mistake or possible something else that I need to do, check, configure from the google project console end which may be required in order to make this work okay. What additional things I need to code, or configure for this.
Kindly tell me please.
I have some more customization to do on this scenario but first things first I want to fetch and show them before doing anything else.
Thanks a lot in anticipation.
There are more wonders in this world of ours than you can wonder; and it is nice to sometimes wonder at them.
Contributor
2173 Points
921 Posts
Re: Youtube Video Data API and MVC Razor Coding checkpoint ---
Feb 02, 2017 07:34 AM|jimmy69|LINK
Hello PGChoudhury,
i'm not an expert, you code seems to be code ...
i've never try the google API youtube but if the API is set to true on the console normally all will be work ...
i've found an article about youtube video data looks at it perhpas it can help you
http://www.c-sharpcorner.com/UploadFile/9b86d4/list-videos-from-a-channel-using-youtube-api-v3-in-C-Sharp/
hope this help
All-Star
17602 Points
3510 Posts
Re: Youtube Video Data API and MVC Razor Coding checkpoint ---
Feb 03, 2017 04:09 AM|Chris Zhao|LINK
Hi PGChoudhury,
I have tried your code, it throws keyInvalid exception.
Best Regards,
Chris
Member
44 Points
249 Posts
Re: Youtube Video Data API and MVC Razor Coding checkpoint ---
Feb 06, 2017 03:06 PM|PGChoudhury|LINK
Hi @jimmy69 & @ChrisZhao:
Thanks to both of you for replying.
I've changed the code some as per jimmy69 's suggestion.
I'm now using
var srchListRqst = yt.Search.List("snippet");
srchListRqst.ChannelId = "UCIpaQzkTUSPsovR2lb1nFog";
compared to the previous
channelsListRequest.ForUsername = "UCIpaQzkTUSPsovR2lb1nFog";

This does work now. As it appear, since I had not used any custom username for my channel (in meaningful name format) it was only my channelID. And not username.
Getting the results nicely now, very satisfied with this.
Thanks to both!
There are more wonders in this world of ours than you can wonder; and it is nice to sometimes wonder at them.