This week we had the opportunity to do some integration with YouTube for one of our clients - upload, edit, remove and displaying
videos all in the "comfort" of their own website.
Initially we thought that we will probably need to do this using raw calls etc to YouTube
(which we'd probably wrap into some sexy reusable classes), but luckily Google did all the work for us already via
their data API - in a few lines of code one can
for example upload a video to YouTube (like you will see in the following post).
Before you continue, you need to download the Google Data API (if you
didn't already) and register for a YouTube developer key, you'll also need
a username and password to access YouTube.
Lets jump into some code...
You'll need to add the following Google API references to your solution.
The following namespaces (located within the added references) are required:
using Google.YouTube; using Google.GData.YouTube; using Google.GData.Client; using Google.GData.Extensions; using Google.GData.Extensions.MediaRss;
YouTubeRequestSettings settings = new YouTubeRequestSettings("applicationName", "developerKey", "userName", "passWord");
YouTubeRequest request = new YouTubeRequest(settings); Video newVideo = new Video(); newVideo.Title = "Test"; newVideo.Tags.Add(new MediaCategory("Animals", YouTubeNameTable.CategorySchema)); newVideo.Description = "Testing Testing Testing"; newVideo.YouTubeEntry.Private = false; newVideo.YouTubeEntry.MediaSource = new MediaFileSource("C:\\test.wmv", "video/x-ms-wmv"); request.Upload(newVideo);
((GDataRequestFactory)request.Service.RequestFactory).Timeout = 9999999;
public IEnumerable<Video> ListMyVideos() { YouTubeRequest request = new YouTubeRequest(settings); YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultUploads); Feed<Video> feed = request.Get<Video>(query); return feed != null ? feed.Entries : null; }
public Video GetMyVideo(string uploader, string videoID) { YouTubeRequest request = new YouTubeRequest(settings); Uri uri = new Uri(String.Format("http://gdata.YouTube.com/feeds/api/users/{0}/uploads/{1}", uploader, videoID)); return request.Retrieve<Video>(uri); }
public void Remove(Video video) { YouTubeRequest request = new YouTubeRequest(settings); request.Delete(video); }
public void Update(Video video, string title, string description) { YouTubeRequest request = new YouTubeRequest(settings); video.Title = title; video.Description = description; request.Update(video); }
<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/somevideoID" frameborder="0" allowfullscreen></iframe>
quick question May 18, 2011 by Anonymous
I added the references to my c# project as you suggested. I see them in my solution explorer under references... I got my developer code as you also suggested. I then do the following: string YouTubeDevKey = "mydevkey"; YouTubeRequestSettings settings = new YouTubeRequestSettings("mydevname", YouTubeDevKey, "myname", "mypwd"); private void SaveToYouTube() { YouTubeRequest request = new YouTubeRequest(settings); Video newVideo = new Video(); newVideo.Title = "Test"; newVideo.Tags.Add(new MediaCategory("Animals", YouTubeNameTable.CategorySchema)); newVideo.Description = "Testing Testing Testing"; newVideo.YouTubeEntry.Private = false; newVideo.YouTubeEntry.MediaSource = new MediaFileSource("C:\\ubloktest.wmv", "video/x-ms-wmv"); request.Upload(newVideo); } and I get an error from my compiler telling me Error 1 The type or namespace name 'YouTubeRequestSettings' could not be found (are you missing a using directive or an assembly reference?) Is there anything else I need to add to my code to make this work??? something like a using directive??? Thx