KnowlegeZone.com, Share knowledge Gain knowledge
     

Home > ASP : Youtube API using (C# VB.NET)

Article By: Saqib Khan   Date: 3/30/2009

Youtube API using (C# VB.NET)

Below you will Find Code how to use Youtube API using Asp.net (VB.NET).

its a shame all the examples that youtube provided were in c#, and i spent hours finding code/examples in VB.NET but was to no avail. At the end i just used c# version of library that Youtube provided, but my aspx page was in VB.NET.

 

Here is what i did.

1 - Create Two folders within asp_code directory (vb and cs)

2 - Copy c# version of Youtube Api Example within cs Directory, and keep your other code in vb directory.

3 - in your vb.net aspx page, just reference to your c# class.

Below is a example how to pull all related videos of a certain video.

// Thats in cs directory

public static class ListVideos {

    public static IEnumerable<Video> Related(string videoid)
    {
        Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/" + videoid);
        YouTubeRequest request = GetRequest();
        Video video = request.Retrieve<Video>(videoEntryUrl);
        Feed<Video> feed = null;
        feed = request.GetRelatedVideos(video);
        return feed.Entries;
       
    }

}

And this is how you can call this within aspx page:

<asp:label style="display:none;" runat=server ID="vid"></asp:Label>

                    <asp:DataList Style="td.vertical-align:top" HorizontalAlign=Center RepeatLayout=Table DataSourceID="SearchYouTubeSourceRelated" runat=server RepeatColumns=5 RepeatDirection=Horizontal ID="sims">
                
                 <ItemTemplate>
                
                       
                       <a href='/watch.aspx?url=<%#eval("WatchPage") %>'>
                  <img alt="<%#eval("Title") %>" width=120 src="http://img.youtube.com/vi/<%#Imgurl(Eval("id")) %>/2.jpg" border=0" />
             </a><br />
             <%#Left(Eval("Title"), 22)%>...
                 </ItemTemplate>
                   
                
                 </asp:DataList>
                
                 </p>
                
          <asp:ObjectDataSource ID="SearchYouTubeSourceRelated" runat="server" SelectMethod="Related" TypeName="ListVideos">
            <SelectParameters>
                <asp:ControlParameter ControlID="vid"  DefaultValue="bollywood" Name="videoid" PropertyName="Text"
                    Type="String" />                    
            </SelectParameters>
          </asp:ObjectDataSource>

I hope this will make some sense, if you need other examples such as retrieve comment's, Paging (which never works 100%), search categories, please let me know.

Thanks

 

 - Saqib Khan


Share |

Comments.
Comment/Solution Posted By: Jason    Date: 4/6/2009 2:53:00 PM
Works like a Charm

Comment/Solution Posted By: Ali    Date: 4/6/2009 2:55:00 PM
Thank you Saqib. I love it.

Comment/Solution Posted By: Charlie    Date: 4/12/2009 2:21:00 AM
How would you use the API to play a user's YouTube Playlist, and include the playlist thumbnails with the player?

Comment/Solution Posted By: Saqib    Date: 4/12/2009 3:48:00 PM
Youtube API gives you a Option to query only a Specific user, and then Pulling Images is Easy.

Two Query a User, Do this:
http://gdata.youtube.com/feeds/api/users/username/uploads

Details Here:
http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#User_Uploaded_Videos


This is how you specifiy Thumbnail Path:

http://img.youtube.com/vi/CO8zuNQIRHk/2.jpg

where only thing you need to change is "CO8zuNQIRHk", which is the Video ID of the Clip.

See Example Here:
http://www.khanzone.com/Bollywood/movies/songs/downloads/8X10-TASVEER/547/media

Let me know if you need more help.

- Saqib


Comment/Solution Posted By: feroz    Date: 4/30/2009 4:25:00 AM
hi s khan
i need to know where from i will copy youtube api

Comment/Solution Posted By: Saqib    Date: 4/30/2009 12:38:00 PM
Here..
http://code.google.com/apis/youtube/2.0/developers_guide_dotnet.html


Pages :
Post Comment or Solution   OR   Sign in using Google/Aol/Yahoo/Msn/OpenID
Your Name:  
Your Email:  
Comments:
 
Please Verify:
(Type Answer)