Metro Nuggets

Bitesized tidbits for building Modern (Metro) apps.

Introducing Bex, A PCL Library for the MS Health APIs

Yesterday Microsoft [finally] released the MS Health APIs they’d talked about at this year’s Build conference. These APIs are a way of accessing the data that’s been accrued from your Microsoft Band, so any bike rides, runs, sleep tracking, etc. These APIs are a set of REST endpoints that, once authenticated, you can call to access this data. So, naturally, I’ve decided to make this easier.

The Solution

Bex is a PCL library that allows you to easily connect to the MS Health APIs. It contains methods to ease signing users into their Microsoft Account, then provides methods to pull back their data.

Logging In

If you’re using the Live SDK and already have your login scenario handled, then most of this can be ignored. If that is the case, you need to create an instance of LiveIdCredentials and provide the access token you’ve already acquired for the user; then just call IBexClient.SetCredentials() and pass in that LiveIdCredentials instance.

For the rest of you, I provide the oAuth url required so that you can send the user to the Microsoft Login page, all you need to do is provide the list of scopes that you wish to have access to, and, if required, the redirect url.

var url = App.BexClient.CreateAuthenticationUrl(new List<Scope>
{
    Scope.ActivityHistory,
    Scope.ActivityLocation,
    Scope.Devices,
    Scope.Profile
});

WebAuthenticationBroker.AuthenticateAndContinue(
    new Uri(url), 
    new Uri(BexClient.RedirectUri), 
    new ValueSet(), 
    WebAuthenticationOptions.None);

If you’re building a Windows phone, Windows, or UWP app, leave the redirect url blank and use the WebAuthenticationBroker; there is a constant on BexClient for the internal RedirectUrl being used (see the sample project). The above code is taken from a Windows Phone 8.1 app.

Once you have been returned to the app and got the code from the Microsoft authentication, you need to exchange this code for an access token, which you do so using IBexClient.ExchangeCodeAsync(). If successful, this will automatically set the credentials internally, but it’s advisable to save them somewhere your app can access them at a later date, this will save the user having to keep signing into your app.

Making the Calls

Once your user is authenticated, you are then free to start requesting the user’s data. An example would be to get the user’s profile information

var profile = await App.BexClient.GetProfileAsync();
if (profile != null)
{
    var dialog = new MessageDialog("Signed in as " + profile.FirstName);
    await dialog.ShowAsync();
}

All fairly simple, and the other methods are no more difficult. There are methods for getting the user’s devices, activities, and summaries with varying filters allowed.

Installing

It’s available to download from Nuget

BexNuget

Source Code

It wouldn’t be a solution from me without the source code being available on GitHub. If you want to build this project from the source code, you will need to be using Visual Studio 2015 as the code in there uses some of the new C#6 syntax.

SL

Advertisements

3 responses to “Introducing Bex, A PCL Library for the MS Health APIs

  1. Pingback: Dew Drop – July 10, 2015 (#2051) | Morning Dew

  2. Pingback: Dew Drop – July 13, 2015 (#2052) | Morning Dew

  3. kris February 14, 2016 at 22:35

    Dear @scottisafool, thanks for that wandering tut. What I wanted to ask is: if the app you share is a windows application (and not windows phone) and if it could be implemented for Unity3D!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: