Accessing Vimeo and YouTube APIs with Xamarin.iOS

The Uptred Source library allows you to build C# based applications for iOS that use Vimeo and YouTube APIs. You can easily authorize a Vimeo or YouTube account, and afterwards query data from the server or even have resumable uploads. This post covers the basics of using Uptred Source to build an application that communicates with Vimeo Advanced API and YouTube API using Xamarin.iOS.

First, you need to create a blank Xamarin.iOS project and add Uptred as a component. You can get Uptred Source either from the Xamarin Component Store, or from the Uptred website. Modify the AppDelegate.cs file to create a new view of type MainViewController:

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
    // create a new window instance based on the screen size
    Window = new UIWindow (UIScreen.MainScreen.Bounds);
    var mainView = new MainViewController ();
    var navigationController = new UINavigationController (mainView);
    Window.RootViewController = navigationController;
    Window.MakeKeyAndVisible ();
    return true;
}

The MainViewController class defines two buttons, one to authorize a Vimeo account and another to authorize a YouTube account. Pushing these buttons simply redirects the user to a scene containing a UIWebView (i.e. AuthViewController) that loads the login page for Vimeo or YouTube.

partial class MainViewController : UIViewController
{
    void BtnAuthVimeo_TouchUpInside (object sender, EventArgs e)
    {
        switchToAuth("Vimeo", VimeoHook.GetLoginURL(
            clientId: Constants.VimeoAPIKey,
            redirect: Constants.VimeoRedirectURL));
    }
    void BtnAuthYouTube_TouchUpInside (object sender, EventArgs e)
    {
        switchToAuth("YouTube", YouTubeHook.GetLoginURL(
            clientId: Constants.YouTubeAPIKey,
            redirect: Constants.YouTubeRedirectURL));
    }
    void switchToAuth(string provider, string url)
    {
        var webScreen = new AuthViewController ();
        webScreen.Provider = provider;
        webScreen.NavigateUrl = url;
        NavigationController.PushViewController(webScreen, true);
    }
}

The AuthViewController scene responds to a URL that contains the code parameter in its query string. In case such page is loaded, the application uses this parameter to authorize the Vimeo or YouTube account. After the account is authorized, the UIWebView is removed from the parent, and the user’s display name is shown on the screen.

if (Provider == "Vimeo") {
    var hook = VimeoHook.Authorize (
        authCode: code,
        clientId: Constants.VimeoAPIKey,
        secret: Constants.VimeoAPISecret,
        redirect: Constants.VimeoRedirectURL);
    InvokeOnMainThread( delegate {
        text.Text = string.Format ("Logged in as {0}!", hook.User ["name"].Value);
    });
}
else if (Provider == "YouTube") {
    var hook = YouTubeHook.Authorize(
        authCode: code,
        clientId: Constants.YouTubeAPIKey,
        secret: Constants.YouTubeAPISecret,
        redirect: Constants.YouTubeRedirectURL);
    InvokeOnMainThread( delegate {
        text.Text = string.Format ("Logged in as {0}!", hook.DisplayName);
    });
}

From this point on, VimeoHook or YouTubeHook classes can be used to send API requests. The Upload method in each class can be used to upload a video file, as explained in the Uptred Source documentation.

You can download the full source code from GitHub!

Leave a Reply