RedCorners SDK for Vimeo

RedCorners SDK for Vimeo is a library that allows you to easily communicate with the Vimeo Advanced API version 3 from your .NET based applications. It can be used to log in, fetch and edit various information and perform resumable streaming uploads.

You can easily get your application set up by registering for some API keys from developer.vimeo.com. Once you have your keys set up, you can start communicating with the Vimeo API using this SDK. In this document, we assume that you have an API Key, an API Secret and a Redirect URL from Vimeo. If you do not have a website and are using a desktop application, you can set your Redirect URL to http://ooze.redcorners.com/. By doing this, users are redirected to a page (shown below) that tells them the verifier code they need to enter in order to complete the authorization process in your application.

 

Creating an Access Token

The first step for start communication with Vimeo is authorization. It can be done in these simple steps:

You need to generate an authorization URL and ask the user to open it and follow the instructions shown there. The authorization URL can be generated like this:

string url = RedCorners.Vimeo.VimeoClient.GetAuthURL(cid: YOUR_API_KEY, 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 a VERIFIER code, which is passed via the code query string variable (e.g. http://ooze.redcorners.com/?code=VERIFIER).

Now you can simply create a VimeoClient object and use it to call Vimeo API methods:

var vc = RedCorners.Vimeo.VimeoClient.Authorize(
authCode: VERIFIER,
cid: YOUR_API_KEY,
secret: YOUR_API_SECRET,
redirect: REDIRECT_URL);

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

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

Logging in Using an AccessToken

To quickly log in with an existing AccessToken, you can create the VimeoClient 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.Vimeo.VimeoClient.ReAuthorize(
accessToken: ACCESS_TOKEN,
cid: YOUR_API_KEY,
secret: YOUR_API_SECRET,
redirect: REDIRECT_URL);

Calling API Methods

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

var response = vc.Request(ENDPOINT, PARAMETERS, HTTPMETHOD, JSONBODY);

ENDPOINT is the API method you want to call (e.g. /me/videos for listing your videos).
PARAMETERS is a dictionary of parameters that you want to pass to the method (e.g. {{“sort”, “date”}} to sort the videos 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 = vc.Request("/me", null, "GET");

And your display name can be retrieved as follows:

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

Uploading a Video

RedCorners Vimeo SDK features a robust and very easy to use uploader. This uploader supports resuming, replacing existing videos, 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 = vc.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 Vimeo.

Uploading a Video with Resume Support

To enable resume support, you need to store your upload ticket somewhere in case something goes wrong. An upload ticket contains three values: The completion URI, the ticket ID and the upload URL. This code shows a fresh upload that can be resumed later:

var ticket = vc.GetTicket();
Settings.Default.CompleteUri = ticket.CompleteUri;
Settings.Default.TicketId = ticket.TicketId;
Settings.Default.UploadLink = ticket.UploadLinkSecure;
Settings.Default.Save();
string video_id = vc.Upload(“D:/Video.mp4”, ticket);

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 ticket:

var ticket = new Ticket();
ticket.CompleteUri = Settings.Default.CompleteUri;
ticket.TicketId = Settings.Default.TicketId;
ticket.UploadLinkSecure = Settings.Default.UploadLink;
string video_id = vc.Upload(“D:/Video.mp4”, ticket);

Replacing an Existing Video

To replace a video that already exists on Vimeo, you can simply get a special ticket with its ID:

var ticket = vc.GetTicket(videoId);
string video_id = vc.Upload(“D:/Video.mp4”, ticket);

Upload Progress Notification

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

vc.UploadCallback = UploadCallback;
string video_id = vc.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
}