Consuming Google APIs In UWP XAML / C# Apps

Google APIs and UWP - How does it all work?

Out of curiosity and for my own education, I recently started to look into Google APIs and how a Universal Windows Platform (UWP) App could consume such web APIs.  Specifically, I wanted to hook the YouTube API to easily get access to basic YouTube channel statistic on my Windows phone.

But how to get started?  As usual one turns to Google for research, and I found there was plenty of material available to get started.

I quickly decided that I wanted to have a crack at actually creating a functional app rather than just do some dry research.  After all, applying the knowledge helps with understanding and retention, right? 

Furthermore, I was curious how hard it would be to actually then publish such a working app in the Windows store.  So I set about finding out, but that will be another post in itself.

Accessing Google APIs - Authentication is Key

There is a lot of material on the web on how to access the Google provided web APIs.  Fundamentally, calls to the Google APIs have to be authenticated. And as far as I can see, there are two options:
  1. Use an API Key
  2. Use OAuth2 Authentication
The first method is simple enough to get going, the second one not so easy when you are dealing with a UWP XAML / C# app.  The main reason for that is that the Google code samples and libraries for OAuth2 actually don't work for UWP C# yet.  While you can freely download the libraries and the code will successfully build, it actually fails at run time.

Google mentioned they would address this and fix their code libraries down the track, but there is no time frame from what I can gather.

Hence I concentrated on the first method of using an Authentication Key.  To get started set up an account and log into the Google Developers API Console.  Once in, click on Enable API which will land you on the Library page where you can pick which Google API to actually enable/allow.


In my case I selected the YouTube API for my app.

Then click on Credentials on the navigator on the left, followed by the Create Credentials button.  This will create an API Key and pop up a window containing your new key.  Be sure the restrict and secure that key so others can't steal your volume allocation. And of course don't publish that key in public code repositories by accident.


Needless to say the one in this screenshot is just for illustration purposes and no longer exists so don't bother copying it 😉.

Let's Get Coding - Visual Studio and Google Nuget Packages

I'm not a developer, so I'm not going to pretend I know exactly what I am doing, but for the purpose of this educational exercise it was simple enough to get going.

Grab Microsoft Visual Studio - the free Community Edition will do just fine - and start your XAML / C# UWP project.  I cheated and used the newly available UWP WindowsTemplateStudio project to create a basic, Windows Store compliant UWP app.

Next I downloaded the required Google API Nuget packages, in this case Google.Apis.YouTube.V3 and I think Google.Apis.Services.  I then cobbled together a XAML UI and a button to click to fetch and display basic YouTube Channel Statistics.



Next I put some code into the button click event handler in the page's code-behind.  It's along the lines of the following:


        private void button_Click(object sender, RoutedEventArgs e)
        {
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = "Your-API-Key-From-Google",
                ApplicationName = "BlazingTMYouTubeViewCounter"
            });

            var channelsListRequest = youtubeService.Channels.List("snippet,statistics");
            channelsListRequest.Id = "UCkW0h_mbdmo0YChc0fzMkQw";

            var channelsListResponse = channelsListRequest.Execute();

            foreach (var channel in channelsListResponse.Items)
            {
                var channelViewCount = channel.Statistics.ViewCount.Value;
                textBox1.Text = Convert.ToString(channelViewCount);
            }

            textBox.Text = this.GetType().ToString();
        }


Now that's nowhere near actually the final working code, but you get the drift. You create a new YouTubeService object (part of the Google API Nuget packages code), and set your ApiKey and optional ApplicationName property.

Then you create YouTubeService.Channels.List request object, set a default channel ID and execute the request.  And lastly you have to process the response and display the required values in the UI.

If you are a serious developer, you are probably laughing by now at my description, but hey that's okay.  This is just for illustration purposes.

In any case, I managed to build a basic UWP XAML / C# app which was working fine on my PC and Lumia 930 Windows mobile.

As mentioned earlier in this post, I actually also went on to publish the app in the Windows Store just to get familiar with the process.  If you are curious, search the Windows Store for an app called YouTubeChannelStats, download and install for fun.  Very basic but it works.

How all that was done will be the subject of another post some time.  Hope you enjoyed this one and feel free to comment and share.

Cheers

MB
Get it from Microsoft

Most Popular Posts