Introduction
Question: What would the 21st century be without YouTube? Answer: Quite boring. Apart from all the funny and interesting videos on there, the main reason that 99% of people visit YouTube is because of the music videos. Today, I will show you how to make a very small YouTube application.
The YouTube SDK (Software Development Kit)
Before we can jump in with the code, we cannot. Why? Well, we first need to download the YouTube SDK and Google Data API. The reason why I say we need to download this is because, as with all other SDKs, we first must learn how to work with the particular technologies—we cannot just copy and paste code without understanding how they work. Make sense?
We can download these here.
After you have familiarised yourself with the Google GData API and what it can do and browsed through the samples, you’d have a greater idea of what we will try to accomplish today.
Now, some of you might remember that I created a Facebook application or two throughout the years. You’re probably thinking why am I bringing up Facebook here? The answer is: In order for us to be able to communicate with Facebook or any social media platform from any of our programs making use of their platform(s) we must have a valid developer key. YouTube and Google is not much different. In a previous article utilising Google Maps, we also had to have a developer key. This key proves to whomever that this application is valid and not trying to do illegal stuff. This is how the programs get tracked at the end of the day, even any mobile app needs a certain type of developer key.
How Do I Get a Google/YouTube Developer Key?
You need to have a valid Google account and then navigate to http://code.google.com/apis/youtube/dashboard/gwt/index.html. Here, you can set up a name for your project and get your key. This key must be present in all your programs using this framework, along with the application name you specified here. This ensures that Google can track your program’s calls and usage.
Our Project
The purpose of today’s little sample is to add a new playlist on your YouTube account and add a video to it. It is actually pretty easy. Let’s get started.
Design
Not much of a design. Create a new VB Windows Store app and add one button and a list box to your page. Name your objects anything you desire.
Add the necessary references. You can find all the needed files at these locations:
- C:Program Files (x86)GoogleGoogle YouTube SDK for .NETSamples
- C:Program Files (x86)GoogleGoogle Data API SDKRedist
Code
I hope you have done some reading and browsing through the installed YouTube and Google documentation. If you haven’t, don’t stress because this app is not too complicated…. for a change!
Add the needed Namespaces:
Imports Google.Apis.Auth.OAuth2 Imports Google.Apis.Services Imports Google.Apis.Upload Imports Google.Apis.Util.Store Imports Google.Apis.YouTube.v3 Imports Google.Apis.YouTube.v3.Data
Okay, I went a bit overboard here, but I felt it necessary to show you as much as I can. These namespaces give access to most of YouTube’s functionalities and services. Obviously, to look at each one individually through a magnifying glass is a discussion for another day. Luckily, the complete documentation also gets installed along with the libraries, so the onus is on you to delve through them.
Add the following piece of code:
Private Async Sub btnGet_Click(sender As Object, _ e As RoutedEventArgs) Handles btnGet.Click Dim ytCred As UserCredential Using stream = New FileStream("client_secrets.json", _ FileMode.Open, FileAccess.Read) ' This OAuth 2.0 access scope allows for ' full read/write access to the ' authenticated user's account. ytCred = Await GoogleWebAuthorizationBroker. _ AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, _ New () {YouTubeService.Scope.Youtube}, "user", _ CancellationToken.None, New FileDataStore(Me.[GetType]().ToString())) End Using Dim ytObj = New YouTubeService(New _ BaseClientService.Initializer() With { _ .HttpClientInitializer = ytCred, _ .ApplicationName = Me.[GetType]().ToString() _ }) ' Create playlist in the authorized user's channel. Dim Playlist = New Playlist() Playlist.Snippet = New PlaylistSnippet() Playlist.Snippet.Title = "HTG_PlayList" Playlist.Snippet.Description = "HTG's Playlist" Playlist.Status = New PlaylistStatus() Playlist.Status.PrivacyStatus = "public" Playlist = Await ytObj.Playlists.Insert(Playlist, _ "snippet,status").ExecuteAsync() ' Add a video to the newly created playlist. Dim PlaylistItem = New PlaylistItem() PlaylistItem.Snippet = New PlaylistItemSnippet() PlaylistItem.Snippet.PlaylistId = newPlaylist.Id PlaylistItem.Snippet.ResourceId = New ResourceId() PlaylistItem.Snippet.ResourceId.Kind = "youtube#video" PlaylistItem.Snippet.ResourceId.VideoId = "tGWVVdVbnJc" PlaylistItem = Await ytObj.PlaylistItems.Insert(newPlaylistItem, _ "snippet").ExecuteAsync() lstGet.Items.Add("Playlist item id {0} was added to playlist id _ {1}.", PlaylistItem.Id, Playlist.Id) End Sub
Any layman should be able to understand most of the preceding code. Let me explain it anyway, because I am such a nice guy… 🙂
I first made use of the GoogleWebAuthorizationBroker object’s AuthorizeAsync method to determine whether or not this is a valid application. Remember, in my Facebook article I referenced earlier, that I spoke about the importance of OAuth; well, here it comes into play again. Once it is established that the application is indeed valid, it needs to check whether or not it is dealing with a valid user. If you do not have a valid YouTube account, obviously this app won’t go further, so it is crucial that you at least create a YouTube account before running this app.
It then determines the application’s name, as well as its intended purpose. This was probably the most difficult part to do.
Next, I created a Playlist object. This object allows you to create a new playlist. A playlist is a set of your favourite videos that you’d like to watch over and over again. You set its various (quite easy) properties, and send the request back to YouTube via the ExecuteAsync method.
Obviously, a playlist is useless without any items. The next object I created was a PlaylistItem object. This identifies the physical video you’d like to add to your playlist.
Conclusion
Thank you for always reading my articles. Although some may be a bit shorter than others, the complexity or the technologies involved makes it all worthwhile. Until next time, cheers!