locked
Youtube Video Data API and MVC Razor Coding checkpoint --- RRS feed

  • Question

  • User-215451226 posted

    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.

    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.
    Frown Frown


    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.

    Wednesday, February 1, 2017 4:37 PM

Answers

All replies

  • User632428103 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 2, 2017 7:34 AM
  • User-2057865890 posted

    Hi PGChoudhury,

    I have tried your code, it throws keyInvalid exception.

    Best Regards,

    Chris

    Friday, February 3, 2017 4:09 AM
  • User-215451226 posted

    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.
    Smile Smile Smile

    Thanks to both!

    Monday, February 6, 2017 3:06 PM