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

On this page...

Tags

Calendar

<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

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