This sample program demonstrates how to use several of the Google APIs with YouTube. It does not demonstrate every API. Some of the techniques demonstrated in this sample program can be used with other Google API Client Library for .NET V3 classes.
Some of the Google APIs Client Library for .NET V3 classes demonstrated in this sample program are as follows:
Because some of the Google APIs Client Library for .NET operate asynchronously (i.e., separate threads), it is important to understand how to interact with Windows Forms Controls since cross-thread access is not allowed.
This sample was developed using Visual Studio 2013.
Using the Code
Installing the Source Code and Libraries
- Before attempting to use this sample program, ensure that all Microsoft Windows Update patches have been installed.
- Obtain a client secret json file by registering on the Google Developers Console at https://console.developers.google.com
- After opening the solution for the sample program in Visual Studio, there will be a lot of errors in Visual Studio's Error List window. This is because the Google APIs Client Library for .NET NuGet packages have not been distributed with this sample program.
To install the Google APIs Client Library for .NET in Visual Studio NuGet packages, click on Tools / NuGet Package Manager / Package Manager Console and enter the following command:
- After the NuGet package install is complete, click Tools / NuGet Package Manager / Manage NuGet Packages for Solution, then click on Updates / nuget.org and install all of the update packages.
- Edit the
GetCredentials subroutine to insert your
ClientSecret. These are found in the client secret json file. This sample program shows how to embed the
ClientSecret in the source code. There are other samples provided by Google that show how to read
ClientSecret from the client secret json file.
Executing the Sample Program
Using the program to demonstrate the APIs consists of six steps.
- Select File: Using a OpenFile dialog window, select the video file to be uploaded.
- Get Credentials: Using OAUTH2 protocol, obtain a token file and then retrieve the YouTube channel and channel description. The first time
GoogleWebAuthorizationBroker.AuthorizeAsync is called, a browser window is instantiated and you are prompted for the username and password of your YouTube channel. Upon entering the correct username and password, a token file is downloaded to the C:\Users\<YourWindowsUserName>\AppData\Roaming\Google.Apis.Auth directory. Subsequent calls will refresh the token file if necessary.
- Get Categories: Retrieve the list of video categories, insert each category id and category title in a custom class and add the custom class to the Item list of a
- Upload Video: Select the category and enter the video's title, description, keywords, date recorded and privacy status and then upload the video to YouTube. Sample code is provided to resume the upload in case of a network server issue. I know that resume works when "A task was canceled" error is returned after an
HTTPClient TimeOut error. After every "chunk size" bytes is uploaded, the
ProgressChanged event is fired. In the sample program, when there is a whole percent difference between the last percent complete value and the current complete value, a percent complete line is added to the
ListBox. In this sample program, the "chunk size" is set to .5MB. After a video is successfully uploaded, the Video Id is assigned to the Video Id
- Status: The video's status is retrieved, formatted and displayed in a
TextBox. The sample code that retrieves the video information is designed to retrieve information for multiple videos. In this sample, only the information for the most recently uploaded video is retrieved.
- Delete Video: Deletes the most recently uploaded video.
Points of Interest
Windows Forms Controls are not supposed to be directly accessed across threads. This sample program shows how to add items to a
ListBox and assign a value to a
TextBox from separate threads. The Microsoft examples show how to use a control's
InvokeRequired property to determine if
EndInvoke are required for cross-thread access to a control. This sample program does not use the
InvokeRequired property. Instead, it always uses
EndInvoke when I believed that a control would be accessed in a cross-thread environment.
- Version 1: 29 July 2014 - Initial version
- Version 2: 21 April 2016
IsResumable(), added a test for "A task was canceled" error message to allow retry after an
HTTPClient TimeOut error.
objYouTubeService.HttpClient.Timeout to a value higher than the default.
.ResumeAsync Do..Loop, added
Thread.Sleep() delay using
- In .
ResumeAsync Do..Loop, inserted
Thread.Sleep(3000) delay to ensure
ProgressChanged Event has time to execute and to allow extra time for network and/or server issues to resolve.
.ResumeAsync Do..Loop, inserted a condition to quit after a specified number of retries.
Try..Catch blocks around
.ResumeAsync. Exception handling and reporting is done in the
- Version 3: 6 May 2016
- Modifed the
IsResumable() function so that it always returns
Version 4: 11 July 2016
- Updated to .NET Framework 4.5.1.
- Updated to Google API .NET Client version 1.14.0 (Released 5 July 2016).
- Added code to demonstrate a new feature of Google .NET Client API: Ability to resume an upload after a program restart.
IsResumable() function to test for number of retries and
UploadStatus.Failed case of the
videosInsertRequest_ProgressChanged event handler to set retries to max value if the error contains
Version 5: 26 July 2016
UploadStatus.Failed case of the
videosInsertRequest_ProgressChanged event to improve HTTP Status error code handling on failed upload.