Querying Youtube API using C#

Posted in .NET 2.0 | Google API | YouTube at Thursday, December 20, 2007 10:40 PM UTC

This example shows how to use Google Data API to query and obtain videos from YouTube. There is also a downloadable zip file with the example in ASP.NET / C#.

  1. First of all, you need to download the Google Data APIs Client Libraries.
  2. Second, use the following namespaces:

    using
    Google.GData.Client;
    using
    Google.GData.Extensions;

  3. Use the service by querying by tag, or by search (you can also query related items, etc..):

        //by tag
        //feel free to change number of items, by there is a limit of 50, I believe. 
        //If you want to retreive more, you have to do a loop (retrieve 1-50, then 51 to 100, etc)
        protected void btoGo_Click(object sender, EventArgs e)
    {
    string url = "http://gdata.youtube.com/feeds/videos/-/" + this.txtTag.Text;
    AtomFeed myFeed = GetFeed(url, 1, 20);
    DisplayFeed(myFeed);
    }

    //by search //feel free to change number of items, by there is a limit of 50, I believe. //If you want to retreive more, you have to do a loop (retrieve 1-50, then 51 to 100, etc) protected void btoSearch_Click(object sender, EventArgs e)
    {
    string url = "http://gdata.youtube.com/feeds/videos?q=" + this.txtSearch.Text;
    AtomFeed myFeed = GetFeed(url, 1, 15);
    DisplayFeed(myFeed);
    }
  4. Use the following methods, or similars to get and display the Feed:

        /// <summary>
        /// Create and returns and Google.GData.Client.AtomFee from url with the specific start and number of items
        /// </summary>
        /// <param name="url"></param>
        /// <param name="start"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        private static AtomFeed GetFeed(string url, int start, int number)
    {
    System.Diagnostics.Trace.Write("Conectando youtube at " + url);
    FeedQuery query = new FeedQuery("");
    Service service = new Service("youtube", "exampleCo");
    query.Uri = new Uri(url);
    query.StartIndex = start;
    query.NumberToRetrieve = number;

    AtomFeed myFeed = service.Query(query);
    return myFeed;
    }

    /// <summary> /// Renders feed in example aspx page /// </summary> /// <param name="myFeed"></param> private void DisplayFeed(AtomFeed myFeed)
    {
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    foreach (AtomEntry entry in myFeed.Entries)
    {
    #region render each
    sb.Append("<br /><b>Title:</b> ");
    sb.Append(entry.Title.Text);
    sb.Append("<br /><b>Categories:</b> ");
    foreach (AtomCategory cat in entry.Categories)
    {
    sb.Append(cat.Term);
    sb.Append(",");
    }
    sb.Append(RenderVideoEmbedded(getIDSimple(entry.Id.AbsoluteUri)));
    sb.Append("<br /><b>Published on:</b> ");
    sb.Append(entry.Published);
    #endregion } this.lblResults.Text = sb.ToString();
    }
    private string RenderVideoEmbedded(string idSimple)
    {
    return string.Format("<div id=\"video{0}\"><object width=\"425\" height=\"355\"><param name=\"movie\" value=\"http://www.youtube.com/v/{0}&rel=1\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/{0}&rel=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"></embed></object></div>", idSimple);
    }

Related resources:


Download example web site project in C#:
youtubeAPIExample.zip (65,91 KB)

kick it on DotNetKicks.com AddThis Social Bookmark Button

Wednesday, December 26, 2007 4:35:51 PM UTC
Great info.
What is the better way to retrieve the video´s thumbnail?

thanks
Tiago
Sunday, December 30, 2007 12:48:56 AM UTC
Hi Tiago,

As far as I know, there is not a public access to the thumbnail in the Google Data API. You may have to parse the description field (it is a chunk of HTML) to get the <img html element..

Hope it helps..

Iván
Wednesday, February 06, 2008 8:05:23 PM UTC
how do you get the description
Wednesday, March 26, 2008 12:39:54 PM UTC
Hello,

I can get a Playlist of a User and list all videos for one playlist?... like this: I get all Playlist from a username to a DropDownList, then I select one Playlist and show all videos.

Can help me to make this?

Regards,
LB
Lukas
Thursday, April 10, 2008 7:41:32 PM UTC
takes a long time for sime reason. and i cant access the dll's inside visual studio. i.e. leaves little room for modification. otherwise its a pretty neat example of a websevice
Thursday, April 17, 2008 1:25:00 AM UTC
Is there a way to convert the AtomFeed into a XmlDocument. The problem I am having is with &'s in the AtomFeed.

Thanks
James Arnott
Thursday, April 17, 2008 7:30:41 PM UTC
Hi James,

It has been a long time since I created the example... It is not fresh on mi mind. There should be a simple way to create a XmlDocument from any valid XML structure (and I assume the AtomFeed it is). I am sure there is a way to parse those entities.. Sorry I can not provide a defined way to go..
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, em, i, strike, strong) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview

On this page...

Tags

Calendar

<December 2007>
SunMonTueWedThuFriSat
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

Navigation

Archives

Recommended .NET blogs

The author

Iván Loire
Iván Loire (Zaragoza, Spain)

View Ivan Loire's profile on LinkedIn
Microsoft Certified Trainer
Send mail to the author(s) E-mail

Syndication

Feed Icon