Working with Google Fit 1

What is Google Fit?

Google Fit is a service from Google where you can keep Fitness related data from your Android devices and other connected services.  There are three data types, and can be found from this link https://developers.google.com/fit/android/data-types.

Today I am leaving my note to fetch data from Google Fit for my new project.

There are 3 types of data types

  • Public data types
  • Private custom types
  • Shareable data types

Public data types are standard data types from Google Fit.  List of data type can be found here at https://developers.google.com/fit/android/data-types#public_data_types.  Most of data I want to retrieve is here and that’s what we will be fetching for this article.

Private custom types are data types not defined in public data types and data types that apps defined.  Apps can define their own and retrieve them using their package name.

Shareable data types are Google approved custom types.  If you defined data types worthy to share with others and not in public data types, you can make request to Google and make that as shareable data types.

Now off to fetching data. Let’s take a look at API document here at https://developers.google.com/fit/rest/ .

Google Fit REST API let you

  • Create, obtain, list and modify data sources.
  • Create, obtain, aggregate and delete data-sets.
  • List data points and add them to a data-set.
  • Create, list and delete sessions.

Like other API from Google, we need to obtain OAuth access tokens and invoke the API methods using HTTP request.

Follow these steps (link) and get client ID and secret for OAuth 2.0.

You should have Credentials like below;

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_120516_083640_am

You ll need to set your own Authorized JavaScript origins (where you will be accessing API from) and call back URL.

You can go to https://developers.google.com/oauthplayground/ , test and play with API like below.  I have sent request https://www.googleapis.com/fitness/v1/users/me/dataSources  like below.

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_120416_115810_pm

OK. it seem to be working fine.

Now, let’s go into your program and fetch data source.

I am going to go ahead and create a controller called GoogleFitManage.cs where I ll put most of my code to manage connection with Google Fit.

The flow will be like this; when you come to the GoogleFitManage/Index , I will run authentication with Google Server to see if you are authorized or not.

  • if not, I will redirect you to Google to grant me permissions
  • if yes, I will go and fetch list of data sources you can access

Here is my GoogleFitManageController.cs.  As you can see you need get class libraries Google.Apis.Fitness.v1, Google.Apis.Services, Google.Apis.Auth.Oauth2.Mvc from NuGet prior.

And I have another class which inherit Google.Api.Auth.OAuth.Mvc.FlowMetadata, to set permissions and store token response.

Now I am going to create AuthCallbackController.cs

And last, I am going to create simple View, GoogleFitManage/Index.cshtml to show a list of data source.

This should do it.  Let’s run this and check.

When I run, I am prompted with Google Login Page, (or select account from already logged in Google Accounts).

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_120516_085652_am

Then after log into Google, Google ask me to grant permission to followings,

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_120516_091250_am

Then I get (at my GoogleFitManage/Index.cshtml)

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88_120516_091534_am

Great, working.

Next I want to look deeper into data I can retrieve.

2 Replies to “Working with Google Fit 1”

  1. Hi Sho, Can you please share the github version of your project. I think it would be really helpful.. Love your post but am a newbie and am facing challenges in implementing the same.
    Regards,
    Shef

  2. I implemented exact same code as yours. But at runtime after approving rights for a gmail account to get information I am getting an error :-

    Exception Details: Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:”invalid_request”, Description:”client_secret is missing.”, Uri:”” .

    I am using a personal gmail account ***@gmail.com to get APIClientId and APISecret.

    Thanks
    Neel

Leave a Reply