Friday, March 29, 2013

YouTube API with .NET
How to pull list of videos uploaded by a specific user on YouTube?

This post is next in series of our learning YouTube API with .NET. In the last post we learn about pulling single video metadata using .NET and YouTubeService. In this post we will learn to pull list of videos uploaded by a specific user on YouTube.

For demonstration purpose I have designed following interface in .NET in Visual Studio. We have a text box where we can enter YouTube user id. We have a button control “Show Video List”. On the button click event we have written .NET code logic which interacts with YouTubeServices and pulls list of videos uploaded by the specific user. We have a list view control in which we will show the video list return by the YouTubeService feed.

We have added Google Data API DLLs reference in the application. The code snippet on this form is following.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.YouTube;
using Google.YouTube;
using Google.GData.Client;
using Google.GData.Extensions.MediaRss;

namespace YouTubeAPI
{
    public partial class Form1 : Form
    {
        string myDeveloperKey;
            
        public Form1()
        {
            myDeveloperKey = "my Long string dev key";
        
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            listView1.Columns.Add("Video Title");
            listView1.Columns.Add("Uploaded By");
            listView1.Columns.Add("Uploaded Date");
            
            listView1.FullRowSelect = true;

            string youTubeUserID;

            youTubeUserID = textBox1.Text;
            
            YouTubeService myservice = new YouTubeService("aa",myDeveloperKey);

            Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/users/" + youTubeUserID + "/uploads");
        
        
            FeedQuery fq=new FeedQuery();
            fq.Uri=videoEntryUrl;
            
            Feed<Video> videoFeed = new Feed<Video>(myservice,fq);
                        foreach (Video entry in videoFeed.Entries)
            {
                listView1.Items.Add((new ListViewItem(new string[] { entry.Title.ToString(), entry.Author.ToString(), entry.Updated.ToLongDateString() })));
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}

After setting up the design and code in the visual studio, I connected with the internet and navigate to YouTube website. From the home page I copied a user id as I want to test the program to return the list of videos uploaded by the specific user.

I pasted the user id in the text box and clicked on the “Show Video List” button. The code logic written on the button control works as expected and it returns me the list of 25 videos uploaded by the specific user.

Popular Posts

Real Time Web Analytics