YouTube API Getting Started C# Code

.NET
11 Shares
YouTube API Getting Started C# Code

I wanted to create an application for updating YouTube playlists, so I started using the default service oriented YouTube API, provided by Google. To plug it in my WPF tool, I built an SDK.
Here I will give you guidelines how to start using it. In future articles, I will provide you more detailed information about the API and the different methods in it.

Getting Started YouTube API

First you need to create an account in Google if you don’t have already. Next you should navigate to Google Developers Console site, located at the following URLhttps://console.developers.google.com
Next you should create a new project from the initial page.
Google Developers Console Create New Project

Click on the created link with your project’s name. Click on the “Enable an API” button that will lead you to a new page where you can enable all needed APIs.
Enable an API button

On the next page enable “YouTube Data API v3” and “YouTube Analytics API“.
Enable YouTube API buttons

The free version of the APIs provides you a limited quota usage of the service. For side projects, it is enough. You can check what is your API traffic by clicking the link of the name of the API then the “Quota” link.
Quota Usage Google Developers Console

Once you are ready, you should generate an authentication JSON file that your application will use to connect to the YouTube service. To do that navigate back to the Google Developers Console Dashboard and click Credentials link.
Credentials Link Google Developers Console

On the next page click the “Create new Client ID” button to generate the authentication JSON file. After that, you can download it.
Create New Client Id Google Developers Console

Now you have an authentication file. Next you need to add the required DLLs to yours .NET project. You can use the official Google’s NuGet Packages.
Open NuGet Console and search for “Google.Apis.YouTube.v3“.
YouTube API NuGet Package

Once all packages are installed, create a new class- YouTubeServiceClient and add usings to the YouTube API.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

You can use your new service client like singleton adding the code below.

public class YouTubeServiceClient
{
private static YouTubeServiceClient instance;
public static YouTubeServiceClient Instance
{
get
{
if (instance == null)
{
instance = new YouTubeServiceClient();
}
return instance;
}
}
}

In order to create the Google’s YouTube Service and authenticate, first add the downloaded authentication JSON file to your project (client_secrets.json in code example). After that, you can add the following method to initialize the service.

private async Task<YouTubeService> GetYouTubeService(string userEmail)
{
UserCredential credential;
using (var stream = new FileStream(client_secrets.json, FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[]
{
YouTubeService.Scope.Youtube,
YouTubeService.Scope.Youtubepartner,
YouTubeService.Scope.YoutubeUpload,
YouTubeService.Scope.YoutubepartnerChannelAudit,
YouTubeService.Scope.YoutubeReadonly
},
userEmail,
CancellationToken.None,
new FileDataStore(this.GetType().ToString()));
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
return youtubeService;
}

For more detailed information about the YouTube API, please visit the official documentationhttps://developers.google.com/youtube/v3/

.NET Series

YouTube Playlists API C# Code
18/07/2014 / By Anton Angelov
MSBuild TCP IP Logger C# Code
16/03/2014 / By Anton Angelov
7 New Cool Features in C# 6.0
26/10/2014 / By Anton Angelov
Top 15 Underutilized Features of .NET
23/08/2015 / By Anton Angelov

banner