Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
.NET Zone is brought to you in partnership with:

Amir Ahani (Microsoft Most Valuable Professional) is expert in Microsoft .NET technologies such as C# and he spends most of his time teaching and consulting internationally. He has been teaching advanced Microsoft courses at British Columbia Institute of Technology (BCIT) since 2003. In addition, he is a member of and speaker at the following communities: DevTeach, .netBC, Vancouver Technology Festival, ASQ, APICS, PMI, NPA, and ITAC. Amir is a DZone MVB and is not an employee of DZone and has posted 10 posts at DZone. You can read more from them at their website. View Full User Profile

Retrieve and Update YouTube Content in C#

02.03.2012
Email
Views: 1864
  • submit to reddit
The .NET Zone is presented by JNBridge and DiscountASP.NET to keep you updated on all the latest news, tips, and tools in the .NET community.  Check out today's top .NET content and read about JNBridge's innovative tools for .NET and Java interoperability or watch DiscountASP.NET's videos on the benefits of a hosted and managed TFS server.  
The YouTube Google API for .NET developers can be used in C# or ASP.NET applications to authenticate a YouTube user, to extract information about a single video from a list of videos or a set of search results, to fetch a list of videos, to upload videos to YouTube, to interact with YouTube videos, to access, create and update favorite videos, and to subscribe to YouTube channels.

 

Example 1. Authenticating a YouTube User

YouTubeRequestSettings settings = new YouTubeRequestSettings(“example app”, clientID, developerKey);
YouTubeRequest request = new YouTubeRequest(settings);

 

Example 2.Displaying a Feed of Videos

Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));
printVideoFeed(videoFeed);
static void printVideoFeed(Feed<Video> feed)
{
foreach (Video entry in feed.Entries)
{
printVideoEntry(entry);
}
}

 

Example 3. Searching YouTube Videos

YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);
//order results by the number of views (most viewed first)
query.OrderBy = “viewCount”;
// search for puppies and include restricted content in the search results
// query.SafeSearch could also be set to YouTubeQuery.SafeSearchValues.Moderate
query.Query = “C# Learners”;
query.SafeSearch = YouTubeQuery.SafeSearchValues.None;
Feed<Video> videoFeed = request.Get<Video>(query);
printVideoFeed(videoFeed);

 

Example 4. Uploading Videos to YouTube

Video newVideo = new Video();
newVideo.Title =”Serialization”;
newVideo.Tags.Add(new MediaCategory(“Autos”, YouTubeNameTable.CategorySchema));
newVideo.Keywords = “Programming, C#”;
newVideo.Description = “This video explains how to use Serialization in C#”;
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory(“mydevtag, anotherdevtag”,
YouTubeNameTable.DeveloperTagSchema));
newVideo.YouTubeEntry.setYouTubeExtension(“location”, “Vancouver, BC”);
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(“c:\\Serialization.mov”,
“video/quicktime”);
Video createdVideo = request.Upload(newVideo);

 

Example 5. Adding a Favorite Video to YouTube

YouTube users can choose to mark videos that they watch as favorite videos. A user’s favorite videos feed can be retrieved from the following URL:

http://gdata.youtube.com/feeds/api/users/username/favorites

To add a favorite video, insert a YouTubeEntry object that identifies the video to the authenticated user’s favorite videos feed:

string videoEntryUrl = “http://gdata.youtube.com/feeds/api/videos/CSharplearners”;
YouTubeEntry videoEntry = (YouTubeEntry) service.Get(videoEntryUrl);
service.Insert(new Uri(feedUrl), videoEntry);

 

References
Published at DZone with permission of Amir Ahani, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

This content was brought to you in partnership with JNBridge and DiscountASP.NET.  JNBridge specializes in .NET and Java interoperability while DiscountASP.NET provides TFS hosting, migration, and source control as a service.  Be sure to view JNBridge's brief video series on accessing Java from .NET or check out their tech audit.  For your TFS deployment needs, check out DiscountASP.NET and their resources on TFS migration or see how you like their hosting in their free trial.