Overview

Google Fit helps you to track your fitness data. The Google Fitness REST APIs, on the other hand, allow the developers to extend this further and create customized dashboards. Google Fitness REST APIs are useful if you have a fitness app and you want to integrate your data with Google Fit or if you just want to collect the fitness data and display some information to the users.
In this article, we are going to explore Google Fitness REST APIs and the Google Developer playground to start programming with REST APIs. Please refer to my previous article for the Overview of Google Fit API.

Get Started with REST API

Google Account
The one and important prerequisite is that you should have a Google account. If you do not have, create one from here.
Generate OAuth 2.0 client ID
  1. Open Google API Console from here.
  2. Select any existing project or choose to create a new project.
  3. Click "Continue".

    Google Fitness REST API

  4. Once the project is created, we will have to generate the credentials.
  5. Click “Go to credentials”.

    Google Fitness REST API

  6. Select the options as highlighted below.

    Google Fitness REST API

  7. Click “What credentials do I need?”
  8. Under Authorized JavaScript origins, add https://developers.google.com
  9. Under Authorized Redirect URI, add https://developers.google.com/oauthplayground

    Google Fitness REST API

  10. Click “Create OAuth client ID”.
  11. Set up the OAuth 2.0 consent screen.

    Google Fitness REST API

  12. Click "Continue".
  13. The Client id will be generated. Note it down for future use.

    Google Fitness REST API

  14. Click "Done".

Google Developer Playground

The OAuth 2.0 Playground for Google Developers platform can be accessed from https://developers.google.com/oauthplayground. This playground will help the developers to explore various Google APIs, understand their requests and responses, and query the APIs to fetch the data. In this article, we are going to explore the Fitness APIs out of available loads of various APIs.
  1. Open OAuth 2.0 Playground.
  2. Locate “Fitness V1” API.
  3. Select the scopes under it that you want to query to.

    Google Fitness REST API

  4. Click "Authorize APIs".
  5. In the next screen, choose the Google account to be used.

    Google Fitness REST API

  6. In the confirmation screen, verify the selected scopes.

    Google Fitness REST API

  7. Click "Allow".
  8. In the next screen, click “Exchange authorization code for tokens”.

    Google Fitness REST API
List all Data Sources
Google manages the data through data sources. To see all the data sources, use the below request URI.
  1. https://www.googleapis.com/fitness/v1/users/me/dataSources
Google Fitness REST API
In the response, all the available data sources for the user (me) will be listed along with the data types. derived:com.google.step_count.delta:com.google.android.gms:estimated_steps is one of the example which gives the number of steps.
Get the Number of Steps
We will use the available data source derived:com.google.step_count.delta:com.google.android.gms:estimated_steps to get the steps.
  1. Change the HTTP method to POST.
  2. In the Request URI, type https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate.
  3. Click “Enter request body”. Enter the below request body.
    1. {
    2. "aggregateBy" : [{
    3. "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
    4. }],
    5. "bucketByTime": { "durationMillis": 86400000 }, // This is 24 hours
    6. "startTimeMillis": 1546210381932, // Start time
    7. "endTimeMillis": 1547210381932 // End Time
    8. }
    Google Fitness REST API

  4. Click "Close".
  5. Click "Send the request".
  6. The response will appear on the right.

    Google Fitness REST API

  7. Sum up all the intVal (highlighted above) to get the number of steps. Each intVal indicates the steps for an individual activity at a different time within the specified time span.
Get the start date and end date
We can use plain JavaScript to get the start date.
  1. new Date().getTime()
The end date can be 1000000000s less than the start date (which is approx. one day).

Summary

Google Fitness REST APIs allow performing operations on various available data sources. The Google Developer Playground helps start programming with REST APIs to get the actual data.

12 Comments

Join the conversation! Your thoughts help the community grow.

  • On clicking 'Exchange authorization code for tokens', nothing is happening

  • I Have implemented below code to get yesterday steps count for registered users. But sometime few users data returning is null . FitnessAggregate fitnessAggregate = new FitnessAggregate() { StartTimeMillis = new DateTimeOffset(DateTime.Today.AddDays(-1)).ToUnixTimeMilliseconds(), EndTimeMillis = new DateTimeOffset(DateTime.Today.AddMilliseconds(-1)).ToUnixTimeMilliseconds() }; BucketByTime bucketByTime = new BucketByTime() { DurationMillis = 86400000 }; AggregateBy[] aggregates = new AggregateBy[] { new AggregateBy() { DataSourceId = "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps" } }; fitnessAggregate.AggregateBy = aggregates; fitnessAggregate.BucketByTime = bucketByTime; return JsonSerializer.Serialize(fitnessAggregate);

  • Nanddeep Nachan

    Hi Maree, The manually added activity end up getting a merged value.

  • Pls advise, if user doesn't has step for datasoruce example "derived:com.google.step_count.delta:com.google.android.gms:merge_step_deltas" if we need from them.

    • Nanddeep Nachan

      If user has no data recorded, it will simply return null or zero (based on type of content).

    • I used google Fit application on android mobile for testing, I added manual walking activities and call google Fit api to check list of datasource. In typically I must get step or activity from those but what the season or I did something wrong why cannot get them?

  • Is it possible to list fitness details of multiple users using google fit REST API.if not what will be the solution for that

    • Nanddeep Nachan

      The OAuth 2.0 client ID generated here is for a single user. May be try by following these steps for multiple users and invoke the REST API.

  • Google Rest Api Provide any API To call in ios application ? Ans Me fast

    • Nanddeep Nachan

      REST APIs are platform independent.

  • How i can use it in IOS application Devlopment?

  • Great article. Can you return the google map route( longitude and latitude) that shows in google fit when you record a walk or run.