问题
Existing script for step count works perfectly and I can get the data from 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]);
}
Now I am trying to change this code to get by changing below 2 lines
"dataTypeName": "com.google.weight.summary",
"derived:com.google.step_count.delta:com.google.android.gms:merge_weight"
But I get an error at the response statement. Get an error stating
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")
Could not find what to define for the FetchURL to get the weight data. I coul dnot find any examples on getting weight. As mentioned earlier the code works perfectly fine to get step count. Any help is appreciated.
回答1:
Solved: There was a typo in my dataSourceId. It should have been as below
"dataSourceId": "derived:com.google.weight:com.google.android.gms:merge_weight"
And when getting the weight data, we need to use fpVal instead of intVal
var steps = json.bucket[0].dataset[0].point[0].value[0].fpVal;
Thanks to Robert who provided the original script on his blog and also pointed out my mistake. Link to blog article is here https://ithoughthecamewithyou.com/post/export-google-fit-daily-steps-to-a-google-sheet
来源:https://stackoverflow.com/questions/52233038/how-to-get-weight-data-from-google-fitness-api