Search code examples
javascriptgoogle-apigoogle-fitgoogle-api-js-clientgoogle-fit-api

Issues adding sessions and information to the Google Fit REST Api using JS


So I am reasonably new to using API's with Js but I am struggling a lot to understand how the Google Fit API works. I am attempting to add a new Workout's data to the API by adding a session and some data for the intensity (heart points) of the session. I can get the session to appear correctly but run into constant errors when I try to create a dataSource and add a point to it for the session. It would be greatly appreciated if someone could help me to fix my code to achieve this or could direct me to a more thorough example of similar code as the API docs don't seem to be too well detailed with examples etc. Thanks in advance.

Here's the 3 api calls that I have written so far, one for creating the DataSource, one for the DataPoint and one for the Session. The session works correctly and adds a session of 1 hr for the correct activity but I am unable to get any of the other API requests to work.

Data Source :

``gapi.client.fitness.users.dataSources.create({
             "userId":"me",
             "resource": {
                "application": {
                "name": "LittleWorkouts"
              },
              "dataType": {"field":[{
                "format": "floatPoint",
                "name": "com.google.heart_minutes"
              }],


                "name": "com.google.heart_minutes"
              },
              "device": {
                "manufacturer": "op",
                "model": "6",
                "type": "phone",
                "uid": "1000019",
                "version": "1"
              },
              "type": "raw"
             }
         })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error 1", err); });
``

Data Point :

``
    gapi.client.fitness.users.dataSources.datasets.patch({
      "dataSourceId":"raw:com.google.heart_minutes:292824132082:op:6:1000019",
      "userId": "me",
      "datasetId": "1592087806561000000-1592287806561000000",
      "resource": {
  "minStartTimeNs": "1592087806561000000",
  "maxEndTimeNs": "1592287806561000000",
  "dataSourceId": "raw:com.google.heart_minutes:292824132082:op:6:1000019",
  "point": [
    {
      "startTimeNanos": "1592087806561000000",
      "endTimeNanos": "1592287806561000000",
      "value": [
        {
          "fpVal": 89.1
        }
      ],
      "dataTypeName": "com.google.heart_minutes"
    }
  ]
}
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error 2", err); });
``

Session :

``gapi.client.fitness.users.sessions.update({
            "userId":"me",
            "sessionId": "someSessionId19",
            "id": "someSessionId19",
            "name": "Awesome Workout19",
            "description": "A very intense workout",
            "startTimeMillis": new Date().getTime() - 3600000,
            "endTimeMillis": new Date().getTime(),
            "version": 1,
            "lastModifiedToken": "exampleToken",
            "application": {
                "detailsUrl": "http://example.com",
                "name": "LittleWorkouts",
                "version": "1.0"
            },
            "activityType": 21,
            "activeTimeMillis": 3600000
            }).then((res) => {console.log(res)});
            console.log('res')
        //request.execute((res) => {console.log(res);console.log('executrd')})


        console.log(auth2.currentUser.get().getBasicProfile().getGivenName());

        var request2 = gapi.client.fitness.users.sessions.list({
            "userId":"me"
        }).then((res) => {console.log(res)})
``

Error message

{message: "Unable to fetch DataSource for Dataset: raw:com.google.heart_minutes:292824132082:op:6:1000019", domain: "global", reason: "invalidArgument"}


Solution

  • Awesome, so after some support in the comments I have some working code to add a new session with data from a previously defined data source using 3 API calls. The first call is to create a data source and only needs to be run once. The second and third then add a data point to a data set and creates a new session for the workout respectively. Here's the final working code:

    Data Source:

    /*
             gapi.client.fitness.users.dataSources.create({
                 "userId":"me",
                 "resource": {
                    "application": {
                    "name": "LittleWorkouts"
                  },
                  "dataType": {
                    "name": "com.google.heart_minutes"
                  },
                  "device": {
                    "manufacturer": "op",
                    "model": "6",
                    "type": "phone",
                    "uid": "1000020",
                    "version": "1"
                  },
                  "type": "raw"
                 }
             })
            .then(function(response) {
                    // Handle the results here (response.result has the parsed body).
                    console.log("Response", response);
                  },
                  function(err) { console.error("Execute error 1", err); });
      */
    

    Data and Data Set:

    gapi.client.fitness.users.dataSources.datasets.patch({
          "dataSourceId":"raw:com.google.heart_minutes:108881196053:op:6:1000020",
          "userId": "me",
          "datasetId": z,
          "resource": {
      "minStartTimeNs": workoutStartTime * 1000000,
      "maxEndTimeNs": workoutEndTime * 1000000,
      "dataSourceId": "raw:com.google.heart_minutes:108881196053:op:6:1000020",
      "point": [
        {
            "originDataSourceId": "raw:com.google.heart_minutes:108881196053:op:6:1000020",
          "value": [
            {
              "fpVal": 8
            }
          ],
          "dataTypeName": "com.google.heart_minutes",
          "endTimeNanos": workoutEndTime * 1000000,
          "startTimeNanos": workoutStartTime * 1000000,
        }
      ]
    }
        })
            .then(function(response) {
                    // Handle the results here (response.result has the parsed body).
                    console.log("Response", response);
                  },
                  function(err) { console.error("Execute error 2", err); });
    

    Session:

    gapi.client.fitness.users.sessions.update({
                "userId":"me",
                "sessionId": id,
                "id": id,
                "name": "Morning Workout",
                "description": "A very intense workout",
                "startTimeMillis": workoutStartTime,
                "endTimeMillis": workoutEndTime,
                "version": 1,
                "lastModifiedToken": "exampleToken",
                "application": {
                    "detailsUrl": "http://example.com",
                    "name": "LittleWorkouts",
                    "version": "1.0"
                },
                "activityType": 21,
                "activeTimeMillis": workoutEndTime - workoutStartTime
                }).then((res) => {console.log(res)});
                console.log('res')