Retrieve and Update YouTube Content in C#
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);
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




