Download: Source
YouTube is the most popular and largest video sharing website on which users can upload, view and share videos. One of the key features of YouTube is the ability to view its videos on other web pages and client applications.
The YouTube APIs allows us to bring the YouTube experience to web pages, applications and devices.
YouTube .NET client APIs can be used by authenticated and anonymous users.
- Anonymous user : Read only standard feeds.(Most Viewed,Top Rated,Watch On Mobile, Most Discussed,Top Favourites,Most Responded, Most Recent etc)
- Authenticated user : Public feeds, private feeds (user's inbox feed), upload, delete and update operations.
In order to user YouTube SDK, we have to perform the following initial steps
- Sign up for the Developer key. A Developer key uniquely identifies a product that is submitting the request. This Key should be provided in every YouTube API request
- Download and install .NET YouTube SDK
Default Location for the installed YouTube dlls
![youtube_youtube_dll_location youtube_youtube_dll_location](https://txlv7q.blu.livefilestore.com/y1mQQQhCoKN1EPlz6quR0RGv6Cnp-Rz5hT_4OCa7GGPvlcNhfC6xCpi05PK6aYBCN250VudjAPnCGWwk71jjqJD3uZ2TbD70dSASTNN3XMVJiJSd0ONGoxAN2KyKxSarhq3wYlqL0hUcA5cKmeIC0sq_Q/youtube_youtube_dll_location_thumb[1].png?download&psid=1)
The following is a sample feed that we can parse manually to get video related information or this could be done easily by using YouTube .NET client APIs (there are three dlls in the package).
![youtube_atom youtube_atom](https://txlv7q.blu.livefilestore.com/y1mFb0_Bw5A3k7vtWulpXOMt4iIN9zoY89dXHqgpO9QTbiS2KeenOWZMBOcM6yMrx7rSslg-lkTXFnvhbn5A8fBxUu7hmtx6NRv_8FGFXFPCW1_Zfmh_HD_RTJUBRsyY7VJkvkNmsmPI30fUvKfm0JSBw/youtube_atom_thumb[1].png?download&psid=1)
Example: Standard Feed - http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed
In this article i am going to walk through a silverlight out of browser app, called silverTube.
This app got following abilities:
- Fetching standard and private feeds,
- Perform keyword search,
- Playing videos, and
- Hint of pagination capabilities.
Before stating the project, lets have a look at the end product.
![youtube_weding youtube_weding](https://txlv7q.blu.livefilestore.com/y1mdnabI8Mw7mu4NY7souMG1ykTlvQ5K1WbK53TxwUtKHLm3LsW4JclkvNsINYrDn7Q2GQp_A5-kGQYV2x_G8tsnQ1Zy92bN01ASMFSd5bSBzOW5WdQ3nTFPpF8i3lDykLPHnnxdLPONiJwCpRB0B2Uyg/youtube_weding_thumb[5].png?download&psid=1)
Open the Visual Studio 2010 or Visual Web Developer 2010 Express with the Silverlight 4 tools installed.
Create a new silverlight project and name whatever you like. (here silverTube)
![youtube_initial_solution_art youtube_initial_solution_art](https://txlv7q.blu.livefilestore.com/y1mo0NtB8mj7eCqKUhljBTNfl9lUWJnz5Rn8ThUUsNLMqshVD8aFT6C4PgUp613_OCVz9ZsFDAU1F3ugbq3HB9O3yMoh1Jy1QfN2ey25mYkPHlZ3u9A_WS9IlKPDOvgnsD7zjtCAORlR6ibefHpuVs6oA/youtube_initial_solution_art_thumb[2].png?download&psid=1)
We already installed the YouTube SDK and now we need to have reference to them in our silverlight application. But silverlight runtime doesn’t allow to include those YouTube dlls in the project!
![youtube_cant_ref_dll youtube_cant_ref_dll](https://txlv7q.blu.livefilestore.com/y1m7K-JhPzkshvq8N6-uoJfoLltRe-cwbTUgmcTXcuP6wr-vnvJcl6to9QZNeGNJ2It1pRc2CivZKzFGLrNFdXomN57rzryZ9vM2R5tmsz-6Tat6Uwy4lzJtB4wR_u5OtS898pWqrXIRl-7vGfbtwtrWw/youtube_cant_ref_dll_thumb[2].png?download&psid=1)
There is a workaround about this issue. We will add those dlls as well as a new WCF Service to our silverTube.Web project, and then our silverlight app can easily call that web service.
Add another reference to System.Runtime.Serialization.dll. The System.Runtime.Serialization namespace contains classes that can be used for serializing and deserializing objects.
Now add the following classes to the silverTube.Web project. Those classes will hold video meta data information.
![youtube_three_classes youtube_three_classes](https://txlv7q.blu.livefilestore.com/y1mxr6_U6s2lKG3-2A3ZJEfIcvKsyCfcRi28ss_A5fWtjl5FdRirB_VmkMDAA_vHfE5avBNGec3r6aqZeg86NVmosybvO3AqgmBRxk3WLftMfuIeSoMo1tAyHsQoj1P34xpWC08SAYU3xgRRP1ACNsx7Q/youtube_three_classes_thumb[3].png?download&psid=1)
We will now add a Silverlight-enabled WCF Service to this project and name it as svcSilverTubeServer.svc. Here i am using Silverlight-enabled WCF service template over the standard WCF Service because it is already pre configured and ready to use with Silverlight.
Insert the following namespaces
![youtube_using_namespace youtube_using_namespace](https://txlv7q.blu.livefilestore.com/y1mhWV0eipZfwIyfrueibfwD55PDr12LLN-o1GhYtOmB7wk1zeyawMAq3hw38aeW3RF2Qbd54e-gRIk68Nqqr-QjXlMvidxe8gCKMYMSR_TAGNToWnSm41fK9L81ZbxstLXaeUubQ6SKEXV9qP4mAugpg/youtube_using_namespace_thumb[1].png?download&psid=1)
Before start coding lets have a look at the following diagram that will explain the base concept
![youtube_request_settings youtube_request_settings](https://txlv7q.blu.livefilestore.com/y1mG792hzp2tts7cQc5iFIUpkbKVfM3NiSFp7Ff6V-KkfzvOAH18Zre3x8AsMPqnLDLWS1VOedm3rdh1iQ4MRLs8YKDpHCVHB9hKU3tmIZM1N7S81e0_gVcVpC7YS3mxNAtBpnR-9lVnsvkogWhY1XAdQ/youtube_request_settings_thumb1.png?download&psid=1)
Now we will write a private function called InitializeRequest that takes two parameters
If we provide valid Username and Password, then our intention is to get private feeds and also to perform upload, delete and update operation. Otherwise we could only get public feeds.
![youtube_initialize_request youtube_initialize_request](https://txlv7q.blu.livefilestore.com/y1msemxw84nlw6UOaUz-mWArFp-HEe4cubWKEoOgLuLxR-L9hcHv5WPpmdGqd5zQmmCaFA68ozY3lmxu0jakprrUPeZZCZHQz8gH5gMNX-uh8gFJ8YoYmFLVaMwSGrwmMyqNioDY_QGxH1Dd09xpGD0AA/youtube_initialize_request_thumb[2].png?download&psid=1)
The following is the first public facing web service function for this project.
![youtube_get_standard_youtube_feed youtube_get_standard_youtube_feed](https://txlv7q.blu.livefilestore.com/y1maUeTu3D27zqCgfwJMkVM9weIGJef8rJZzAzVC9Ff25TuXS-8SpXgGFl56yc__zB4JzLhpg-kUzl3ecVxcJz80ivj7FqpvWOnDZWa-AWOzB9Wje42F8l3Ywjx-uFMZXtIUb8iCRy195ltGzApnM_Q5A/youtube_get_standard_youtube_feed_thumb[3].png?download&psid=1)
Our PrepareFeed method takes Atom feed collection and populate our custom collection for the later use
![youtube_PrepareFeed youtube_PrepareFeed](https://txlv7q.blu.livefilestore.com/y1mBiAXyhjQvuEYY8mLvWbA-hhcT6ikAoETmr0PdVW5cMWlU8rdrpwn0qegdBsPsEpaBfmcLk1EVjOa0AYgPcUoxqyzzwhUfeKhWZI0jm11aTgP4I3OTGWDf6ks_Ut589656jrNywRbSvdSYvrY2XQ5AA/youtube_PrepareFeed_thumb[1].png?download&psid=1)
The GetYouTubeByKeyword method allows us to search videos by:
- prmKeyword: a search query term
- prmSafeSearch: Our intention to include all types of videos
- prmOrderBy: Sorting our entries.
- prmVideosBetweenTimeRange: A time period to limit standard feed results.
- prmVideoCategory: Videos by categories.
- prmCurrentPage: Current page no is the starting entry no.
- prmPageSize: Maximum number of entries to return for the current page.
Have look at the method implementation. I guess its well commented.
![youtube_getyoutubebykeyword youtube_getyoutubebykeyword](https://txlv7q.blu.livefilestore.com/y1mD6OxwJAKcBSW10lzodB9Gr7e8x4A_9WNITCORY7CDo67lIcv9hN57xqS4K9vlrHGt9IT9HOCXO6LdI_XIm9w5LSApnLrP5mq8yHXpJvHegQCrMJfLXkJtv6uStFdq1WrZvdc0zix6kM530rEgqnhng/youtube_getyoutubebykeyword_thumb[1].png?download&psid=1)
Now its time to get private feed. Authentication is required for this purpose.
![youtube_GetPrivateFeed youtube_GetPrivateFeed](https://txlv7q.blu.livefilestore.com/y1m0rqy4jcAIQba1m-l0QBnhef9Q8wQNk5BjlLt0zK9g9xRTJliZCQqZtRPGwvUpfDhmhvZyJ6Pv_BEwhlcTkOZVfByQLjW8t-VSFvzs2T1E7qPncUiJN6uWl8E6uiX5qigiMj-oPb_q6K7gEqoazw-ug/youtube_GetPrivateFeed_thumb[1].png?download&psid=1)
At this point we done with our WCF service. Now its time to consume the service in our Silverlight application.
Right-click the silverTube project in Solution Explorer > Select Add Service Reference > Click Discover to get following screen that lists all the supported operations and give a namespace as clientYouTube
![youtube_add_ref youtube_add_ref](https://txlv7q.blu.livefilestore.com/y1mvmftRAP55CIIcp1VKyPiRfmjNS1acQfFnC5xKxXeAEmx3_K8lyMxNdxWPwBNetD5q39mIO2LM8HecAZKRe-CHeoKDd1JEA1OSgNwvrHP2l2GZt1r8F43WWSerBMxxKoJZGvXeQwztQs3AXw_pW8RbQ/youtube_add_ref_thumb[1].png?download&psid=1)
Following XMAL is for our app that could be modified and extended by you. Based on the requirements your XMAL could be different than my one.
![youtube_xml_silvertube youtube_xml_silvertube](https://txlv7q.blu.livefilestore.com/y1mok5KF-sPFmzYIi5zhhOIYFVeX5OCE1T04umSpexk8dJe57p-8Mf9SM9HVWUZa38R77fYezkzzyBj0mFOV-z_XFWDc6BU8-n5BbNA1Mn6Q5zB5lafNusYiK7rSMCT1vWgqhCs26Nc3_Iay88WXV02yg/youtube_xml_silvertube_thumb[1].png?download&psid=1)
ImagePathConverter class:
![youtube_ivalue_converter youtube_ivalue_converter](https://txlv7q.blu.livefilestore.com/y1mf0AY7zy5woWH-tBGnBMA_0arTpd_7cRxJEecshI1Tz49BgoyPvNP_XMjHp0W65A-svPsJcXbbIZW4iI3sUMJXjmCtUOhKqJ8f30-HcaRJ2wnCYDUJblFwnd5SecDfF7gefIJ4DAdlbRuZ8HeRJFt5w/youtube_ivalue_converter_thumb[1].png?download&psid=1)
Code behind for this app is very straight forward. So i am not going to explain here, you better please download the source.
Now we are going to configure the application so that it can be installed for out of browser use.
Right-click the silverTube project in Solution Explorer > Silverlight > Check Enable running applications out of the browser
![youtube_oob youtube_oob](https://txlv7q.blu.livefilestore.com/y1mGe1gV19RumXObLbyguIIGz6ayIDZSp-GkecFGY3yW3uy7_cSWiaXH5i3G2j1A184QMMbdF1OQoDEKuAKT2baEGs4D1lsQ59P1tBRQmu60MSG0WFhZPzPvJYv7CMxYlXxW82wbdqyr3zzd9kpJ0P-XA/youtube_oob_thumb[1].png?download&psid=1)
There is one last thing we need to do is to add clientaccesspolicy.xml file in the silverTube.Web project. This configures the service to allow cross-domain access.
![youtube_clientaccesspolicy youtube_clientaccesspolicy](https://txlv7q.blu.livefilestore.com/y1mfZPQgReKGe6jnIQiYmBxbFucGgpHl8Q67KGkstGMn2-oYlmBuVn51x2DBOyBXnYkkuM3jEUkKfNz2S_wHowlb9Uu_aWDm_BTGoLJELde4gjDsKw-SYA-DyPL-76aupAQvxa4LD7ELb5hwqBLseMeMA/youtube_clientaccesspolicy_thumb[2].png?download&psid=1)
Throughout this article I tried to show you how searching and playing of YouTube videos could be done. In future i will look more into uploading and downloading YouTube videos.
Ref: http://code.google.com/apis/youtube/2.0/developers_guide_dotnet.html
Until than Happy coding.
Sumon Barua
Download: Source
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.