RedCorners SDK for YouTube

Google has a broad range of APIs that you can enable and use, and also an official .NET library. But using this library can be cumbersome, especially when your goal is to have reliable and resumable uploads. RedCorners SDK for YouTube gives you a clean interface to Google APIs, especially those for uploading videos to YouTube, and is focused on giving you a very high flexibility to control your uploads while maximizing the ease of use. Well, below you can see how easy it is to communicate with the Google APIs with the RedCorners SDK.

The first step to take after acquiring the RedCorners SDK is to get your Client ID and Client Secret from Google Developers Console: Once you are logged into the Developers Console, navigate to the Credentials page under the APIs & auth section, and press Create new Client ID.

image

Let’s assume you want to create a desktop app that is going to access Google/YouTube APIs. To do so, choose Installed application and Other as the application type. You will receive three values like the following:

image

The last thing that you need to do on your Developer Console is to enable access to Google+ API and YouTube Data API v3 from the APIs page under the APIs & auth section.

image

 

Creating an Access Token

In order to use your account with the API, you need an access token. To get the access token, you need to generate and open an Auth URL, follow the steps shown on the website and once you are given an Auth Code, you should enter it in the application. The URL can be generated with the code below, where your REDIRECT_URL is urn:ietf:wg:oauth:2.0:oob if you have a desktop app:

string url = RedCorners.YouTube.GetAuthURL(YOUR_CLIENT_ID, redirect: REDIRECT_URL);

At this point, you can either show this URL in your application, or open it for the user. For example to open it automatically in Internet Explorer, you can do this:

var psi = new ProcessStartInfo("iexplore.exe");
psi.Arguments = url;
Process.Start(psi);

This URL asks the user to log in and allow the application to connect to his account. At the end of that process, the browser is redirected to REDIRECT_URL with an AUTH code, which is passed via the code query string variable (e.g. http://ooze.redcorners.com/?code=AUTHCODE). If you have a desktop app, Google will show a page with your AUTH code.

Now you can simply create a YouTubeClient object and use it to call Google/YouTube API methods:

var yc = RedCorners.YouTube.YouTubeClient.Authorize(
authCode: AUTHCODE,
clientId: YOUR_CLIENT_ID,
secret: YOUR_API_SECRET,
redirect: REDIRECT_URL);

This object can be used to call the API methods. This process also generates an AccessToken and a RefreshToken which can be used to relogin quickly in the future without going through this process, so store it somewhere:

Settings.Default.Token = vc.RefreshToken;
Settings.Default.Save();

Logging in Using a RefreshToken

To quickly log in with an existing RefreshToken, you can create the YouTubeClient object like below. Note that the redirect parameter is not necessary if you already have a token and do not wish to perform the authorization process at any time:

var vc = RedCorners.YouTube.YouTubeClient.ReAuthorize(
refreshToken: REFRESH_TOKEN,
clientId: YOUR_CLIENT_ID,
secret: YOUR_API_SECRET,
redirect: REDIRECT_URL);

Calling API Methods

Calling API methods is very simple: First you should look up the parameters and HTTP method for a certain endpoint. Then you can send a request and receive the response by making one simple function call:

var response = yc.Request(url: ENDPOINT, parameters: PARAMETERS, method: HTTPMETHOD, jsonBody: JSONBODY);

ENDPOINT is the API method you want to call (e.g. https://www.googleapis.com/plus/v1/people/me for accessing your Google+ profile).
PARAMETERS is a dictionary of parameters that you want to pass to the method (e.g. {{“sort”, “date”}} to sort items by date).
HTTPMETHOD is the HTTP method you want to use for sending the request. (i.e. GET, POST, PUT, PATCH, etc.)
JSONBODY determines whether you want to pass the parameters as JSON or in query string format.

For example to get your own profile information, you can run:

var me = yc.Request(
url: "https://www.googleapis.com/plus/v1/people/me",
parameters: null,
method: "GET",
overrideApiRoot: "",
host: www.googleapis.com);

And your display name can be retrieved as follows:

string name = me[“displayName”].ToString();

Uploading a Video

RedCorners YouTube SDK features a robust and very easy to use uploader. This uploader supports resuming, large file sizes (64-bit upload) and features a callback for reporting the upload progress. To quickly upload a video with streaming support, you can write:

string video_id = yc.Upload(“D:/Video.mp4”);

That’s it! And it will do the rest, and when the upload is completed, it returns the video id of the uploaded video on YouTube.

Uploading a Video with Resume Support

To enable resume support, you need to store your upload URL somewhere in case something goes wrong. This code shows a fresh upload that can be resumed later:

var url = yc.GetUploadSessionUrl(“D:/Video.mp4”);
string video_id = yc.Upload(“D:/Video.mp4”, url);

Now if something goes wrong during the upload (video_id will be null in this case), the app crashes, or the user cancels the upload, the process can later be resumed by calling the Upload method using the same Url:

string video_id = yc.Upload(“D:/Video.mp4”, url);

Upload Progress Notification

To get notified of the upload progress, you can hook a method to the uploader callback:

yc.UploadCallback = UploadCallback;
string video_id = yc.Upload(“D:/Video.mp4”);

void UploadCallback(VerifyFeedback feedback)
{
Console.WriteLine("{0}/{1} bytes uploaded.", feedback.LastByte, feedback.ContentSize);
}

If feedback.LastByte >= feedback.ContentSize, the video is uploaded successfully and will go to the completion process which leads to returning the video id.

This callback can be used to create multithreaded applications that show a progress bar for their uploads.

Stepped Uploading

To get the maximum control over the upload process, you can enable the stepping mode. This mode uploads one chunk at a time and stops in between, so you can have your own code running after each chunk is uploaded. To see if the upload is completed or not, you can check the return value of the Upload function. If it is null, it means that you still need to continue the uploading process, and if is has a video id, it means that the upload is completed successfully. Moreover, you can use the callback explained in the previous section to get more detailed information about the process.

string video_id="";
while (video_id=="") {
video_id = yc.Upload(“D:/Video.mp4”, step: true);
//Your Code Here
}

Update Video Metadata

The final step of an upload is probably setting the video’s metadata. This includes fields such as Title, Description, Tags, Category, Privacy, and so on. All of these are modifiable via a YouTubeMetadata object:

var meta = new YouTubeMetadata();

meta.Title = "My Video";

meta.Description = "Video Description";

yc.ApplyVideoMetadata(video_id, meta);