Querying Youtube API using C#

Posted in .NET 2.0 | Google API | YouTube at Thursday, 20 December 2007 22:40 GMT Standard Time

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. Secondly, 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);
        //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);
  4. Use the following methods, or similars to get and display the Feed:

        /// Create and returns and Google.GData.Client.AtomFee from url with the specific start and number of items
        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;
        /// Renders feed in example aspx page
        private void DisplayFeed(AtomFeed myFeed)
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (AtomEntry entry in myFeed.Entries)
                #region render each
    Title: "); sb.Append(entry.Title.Text); sb.Append("
    Categories: "); foreach (AtomCategory cat in entry.Categories) { sb.Append(cat.Term); sb.Append(","); } sb.Append(RenderVideoEmbedded(getIDSimple(entry.Id.AbsoluteUri))); sb.Append("
    Published on: "); sb.Append(entry.Published); #endregion } this.lblResults.Text = sb.ToString(); } private string RenderVideoEmbedded(string idSimple) { return string.Format("
    ", idSimple); }
Related resources:

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

AddThis Social Bookmark Button

Wednesday, 26 December 2007 16:35:51 (GMT Standard Time, UTC+00:00)
Great info.
What is the better way to retrieve the video´s thumbnail?

Sunday, 30 December 2007 00:48:56 (GMT Standard Time, UTC+00:00)
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..

Wednesday, 06 February 2008 20:05:23 (GMT Standard Time, UTC+00:00)
how do you get the description
Wednesday, 26 March 2008 12:39:54 (GMT Standard Time, UTC+00:00)

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?

Thursday, 10 April 2008 20:41:32 (GMT Daylight Time, UTC+01:00)
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, 17 April 2008 02:25:00 (GMT Daylight Time, UTC+01:00)
Is there a way to convert the AtomFeed into a XmlDocument. The problem I am having is with &'s in the AtomFeed.

James Arnott
Thursday, 17 April 2008 20:30:41 (GMT Daylight Time, UTC+01:00)
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..
Friday, 13 June 2008 21:44:18 (GMT Daylight Time, UTC+01:00)
Nice work Ivan. I actually just got wind that YouTube provided an API for pulling video into your own site. I'm surprised at how simple this is to do. I noticed there wasn't any library support for .NET at http://code.google.com/apis/youtube/overview.html. I don't understand what the developer key is for? Didn't look like you used one here. It appears to be just using feeds. I'll have to read up on GData FeedQuery and Service objects or disassemble them to see whats going on there. Thanks again for the sample. Do you have any sites you like to share that have more information for us C# users?

Monday, 16 June 2008 03:43:43 (GMT Daylight Time, UTC+01:00)
how i can check video on YouTube if it have ( Confirm Birth Date )
Hi , I'm Thanh
and i'm programmer (C#)
i use api in my app for get info from youtube (for downloading)

it's good work , but i can't get info if video have "Confirm Birth Date " OR No

how can i do that's Plz!
Monday, 23 June 2008 23:32:37 (GMT Daylight Time, UTC+01:00)
Do you have a sample for creating a playlist using YouTube API
Tuesday, 24 June 2008 07:25:09 (GMT Daylight Time, UTC+01:00)
i am Ayaz Hussain sir help me out.
i m n Last semmestor of BCS-7th and i want to make a project and also a little bit easy i can't have any about my project. So i want to give me IDEA'S about project's name and so on

so Please ..........
i wait here for your kind reply..

ur Ayaz
Ayaz Hussain
Friday, 07 November 2008 07:10:54 (GMT Standard Time, UTC+00:00)

What does getIDSimple() in your sample code do?
Can you help me with it
Friday, 07 November 2008 09:29:13 (GMT Standard Time, UTC+00:00)
getIDSimple() will parse the video URL ID provided by youtube and will give you something like "G5iaMbHuuxk", that I called simple ID
Tuesday, 20 January 2009 07:23:16 (GMT Standard Time, UTC+00:00)
Thanks, for the detailed article and the code for searching videos on youtube.
Any how if someone wants the geIDSimple() funtion, here it is.

public string getIDSimple(string url)
string str ="http://gdata.youtube.com/feeds/videos/";

url = url.Remove(0, str.Length);
return url;
Tuesday, 20 January 2009 09:48:11 (GMT Standard Time, UTC+00:00)
I am aware Google has recently published new and improved API's so stay tunned! (see their web site)
Friday, 13 February 2009 13:01:31 (GMT Standard Time, UTC+00:00)

Thank You very much :)

so my search end here :)
Thursday, 23 April 2009 17:29:29 (GMT Daylight Time, UTC+01:00)
how to get the description, your atom feed do not have description hits , views and rating properties but it is in the feed, you are missing many infos.
Sunday, 21 June 2009 20:21:54 (GMT Daylight Time, UTC+01:00)
If anyone is trying to use more than one word in the search, for example instead of drums you want drum fills...try this

string[] keyWords = this.txtSearch.Text.Split(' ');
string url = "http://gdata.youtube.com/feeds/videos?q=";
string keys = string.Empty;

foreach (string k in keyWords)
if (keys.Length == 0)
keys = string.Format("{0}", k);
keys += string.Format("+{0}", k);

url += keys;


Friday, 03 July 2009 13:53:08 (GMT Daylight Time, UTC+01:00)
To use more than one word in the search, you don't need to loop through the search term HttpUtility.UrlEncode(searchTerm) will substitute ' ' by '+' as well.
Wednesday, 27 January 2010 23:17:42 (GMT Standard Time, UTC+00:00)

I think there is a simple way to query youtube API. There is no need to import third party Dll's. For .NET 3.5 you can use SyndicationFeed that is located in "System.ServiceModel.Syndication;" assembly

Code example (search on youtube):

var scrubbed = HttpUtility.UrlEncode(expression);
var reader = XmlReader.Create( string.Format("http://gdata.youtube.com/feeds/videos?q={0}", scrubbed));
var feed = SyndicationFeed.Load(reader);
foreach (SyndicationItem item in feed.Items)
System.Console.WriteLine("The title is {0} and the link to the video is : {1}",item.Title,item.Links[0].Uri);

Thank you,
Thursday, 28 January 2010 09:47:46 (GMT Standard Time, UTC+00:00)
Hi Maricel,

I am sure you are right. There has been a while since I don't look at the API and I know it has been greatly improved.
Don't forget guys to check last version on Google!!


Comments are closed.