如何从Google Fitness API获取体重数据?

时间:2018-09-08 07:27:26

标签: google-fit-sdk

用于步数计算的现有脚本可以完美运行,我可以从Google Fit中获取数据

// see step count example at https://developers.google.com/fit/scenarios/read-daily-

step-total
function getSteps() {
  var start = new Date();
  start.setHours(0,0,0,0);
  start.setDate(start.getDate()-1);

  var end = new Date();
  end.setHours(23,59,59,999);
  end.setDate(end.getDate()-1);

  var fitService = getFitService();

  var request = {
    "aggregateBy": [{

      "dataTypeName": "com.google.step_count.delta",
      "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"

    }],
    "bucketByTime": { "durationMillis": 86400000 },
    "startTimeMillis": start.getTime(),
    "endTimeMillis": end.getTime()
  };

  var response = UrlFetchApp.fetch('https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate', {
    headers: {
      Authorization: 'Bearer ' + fitService.getAccessToken()
    },
    'method' : 'post',
    'contentType' : 'application/json',
    'payload' : JSON.stringify(request, null, 2)
  });

  var json = JSON.parse(response.getContentText());
  var steps = json.bucket[0].dataset[0].point[0].value[0].intVal;

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Weight');
  sheet.appendRow([start, steps]);
}

现在我正在尝试更改此代码,方法是更改​​以下2行

"dataTypeName": "com.google.weight.summary",

"derived:com.google.step_count.delta:com.google.android.gms:merge_weight"

但是响应语句出现错误。出现错误提示

Request failed for https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate returned code 400. Truncated server response: { "error": { "errors": [ { "domain": "global", "reason": "invalidArgument", "message": "datasource not found: derived:<?> (use muteHttpExceptions option to examine full response) (line 39, file "Code")

找不到要为FetchURL定义的内容以获取重量数据。我没有发现任何增加体重的例子。 如前所述,该代码可以很好地获取步数。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

已解决:我的dataSourceId中有一个错字。应该是这样

"dataSourceId": "derived:com.google.weight:com.google.android.gms:merge_weight"

在获取重量数据时,我们需要使用fpVal而不是intVal

var steps = json.bucket[0].dataset[0].point[0].value[0].fpVal;

感谢Robert在他的博客中提供了原始脚本,并指出了我的错误。 链接到博客文章在这里 https://ithoughthecamewithyou.com/post/export-google-fit-daily-steps-to-a-google-sheet