1

Here is the code:

<!DOCTYPE html>
<html>
  <head>
    <title>Google Fitness API</title>
    <meta charset='utf-8' />
  </head>
  <body>
    <p>Get your step counts using the Google Fitness API.</p>

    <!--Add buttons to initiate auth sequence and sign out-->
    <button id="authorize-button" style="display: none;">Authorize</button>
    <button id="signout-button" style="display: none;">Sign Out</button>

    <div id="content"></div>

    <script type="text/javascript">
      // Enter an API key from the Google API Console:
      //   https://console.developers.google.com/apis/credentials?project=_
      var apiKey = 'API_key';
      // Enter a client ID for a web application from the Google API Console:
      var clientId = 'XYZ';
      // Enter one or more authorization scopes. Refer to the documentation for
      // the API or https://developers.google.com/identity/protocols/googlescopes
      // for details. 

      var scopes = 'https://www.googleapis.com/auth/fitness.activity.read';
      var auth2; // The Sign-In object.
      var authorizeButton = document.getElementById('authorize-button');
      var signoutButton = document.getElementById('signout-button');
      function handleClientLoad() {
        // Load the API client and auth library
        gapi.load('client:auth2', initAuth);
      }
      function initAuth() {
        gapi.client.setApiKey(apiKey);
        gapi.auth2.init({
            client_id: clientId,
            scope: scopes
        }).then(function () {
          auth2 = gapi.auth2.getAuthInstance();
          // Listen for sign-in state changes.
          auth2.isSignedIn.listen(updateSigninStatus);
          // Handle the initial sign-in state.
          updateSigninStatus(auth2.isSignedIn.get());
          authorizeButton.onclick = handleAuthClick;
          signoutButton.onclick = handleSignoutClick;
        });
      }
      function updateSigninStatus(isSignedIn) {
        if (isSignedIn) {
          authorizeButton.style.display = 'none';
          signoutButton.style.display = 'block';
          makeApiCall();
        } else {
          authorizeButton.style.display = 'block';
          signoutButton.style.display = 'none';
        }
      }
      function handleAuthClick(event) {
        auth2.signIn();
      }
      function handleSignoutClick(event) {
        auth2.signOut();
      }
      // Load the API and make an API call.
      function makeApiCall() {
        gapi.client.load('fitness', 'v1', function() {
          var request = gapi.client.fitness.users.dataSources.datasets.get({
            userId: 'me',
            dataSourceId: 'com.google.step_count.delta', 
            datasetId: '1476092378000000-' + new Date().getTime() + '000000',
          });
          request.execute(function(resp) {
            console.log(resp);
          });
        });

        console.log(auth2.currentUser.get().getBasicProfile().getGivenName());
      }
    </script>
    <script src="https://apis.google.com/js/api.js?onload=handleClientLoad"></script>
  </body>
</html>

And here is what I get from the console:

Object {minStartTimeNs: "1476092378000000", maxEndTimeNs: "1476461775789000000", dataSourceId: "com.google.step_count.delta", point: Array[0], result: Object} dataSourceId: "com.google.step_count.delta" maxEndTimeNs: "1476461775789000000" minStartTimeNs: "1476092378000000" point: Array[0]

Other than that I don't get error messages in the console. Am I not supposed to get a series of values? If not, how could I do it? I must admit I'm fairly new to APIs :) Many thanks for your help!

2 Answers2

0

Well, make sure that you use valid values for the parameters that you put in your request.

According to the documentation, The dataSources resource includes the data type (and a list of its fields) for each data source. You can specify one of these data types when you create data sources, and you can obtain the name of the data type and a list of its fields when you retrieve a data source from the fitness store.

I also found out that the datasetId is a dataset identifier that is a composite of the minimum data point start time and maximum data point end time represented as nanoseconds from the epoch. The ID is formatted like: "startTime-endTime" where startTime and endTime are 64 bit integers.

For more information, check these related SO questions:

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31
0

****Update***** The line:

dataSourceId: 'com.google.step_count.delta'

in the code above was wrong, as 'com.google.step_count.delta' is a dataTypeName, not a dataSourceId

Works much better with an actual dataSourceId ;) Such as:

dataSourceId: 'derived:com.google.step_count.delta:com.google.android.gms:estimated_steps'