Android
Your app can get a list of food items eaten within a specified time frame by
creating a data read request and querying for DataType.TYPE_NUTRITION
, as
shown in the following example:
val readRequest = DataReadRequest.Builder()
.read(DataType.TYPE_NUTRITION)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build()
For more information about reading data, see Work with the Fitness History.
REST
Retrieving a list of food items eaten, via the REST API is a three stage process:
- Retrieve a list of data sources available for the
com.google.nutrition
data type. Alternatively, if the data source details are already known, these can be used directly in the next step. - Obtain a list of food eaten from each data source in turn.
- (If there is more than one data source) combine the lists of food items within the client application.
Retrieving a list of food data sources
As only the datasource.dataStreamId
is required from each data source,
a field mask can be used, as shown here, to limit the response to just this
property.
HTTP method
GET
Request URL
https://www.googleapis.com/fitness/v1/users/me/dataSources?dataTypeName=com.google.nutrition&fields=dataSource(dataStreamId)
Response
If successful, the response is a 200 OK
status code. The response body
contains a JSON list, each item in the list corresponding to a data source.
For example:
{
"dataSource": [
{
"dataStreamId": "raw:com.google.nutrition:com.example.nutritionSource1:"
},
{
"dataStreamId": "raw:com.google.nutrition:com.example.nutritionSource2:"
}
]
}
CURL command
$ curl \ 'https://www.googleapis.com/fitness/v1/users/me/dataSources?dataTypeName=com.google.nutrition&fields=dataSource(dataStreamId)' \ --header 'Authorization: Bearer ya29.yourtokenvalue' \ --header 'Accept: application/json' \ --compressed
Obtaining a list of food eaten from a data source
Use the dataSource.dataStreamId
from each of the sources in step 1, in
turn, to retrieve the list(s) of food eaten.
The datasetId
is start and end of the required time period, in nanoseconds
as defined in the data set resource.
For example, 1546300800000000000-1546387200000000000
represents the
datasetId
for 01 Jan 2019 00:00:00 UTC to 02 Jan 2019 00:00:00.
HTTP method
GET
Request URL
https://www.googleapis.com/fitness/v1/users/me/dataSources/dataSource.dataStreamId/datasets/1546300800000000000-1546387200000000000?fields=point%2Fvalue%2FstringVal
Response
{
"point": [
{
"value": [
{},
{},
{
"stringVal": "apple"
}
]
},
{
"value": [
{},
{},
{
"stringVal": "banana"
}
]
},
{
"value": [
{},
{},
{
"stringVal": "carrot"
}
]
}
]
}
CURL command
$ curl \ 'https://www.googleapis.com/fitness/v1/users/me/dataSources/dataSource.dataStreamId/datasets/157059699023000000-1575159699023999000?fields=point%2Fvalue%2FstringVal' \ --header 'Authorization: Bearer ya29.yourtokenvalue' \ --header 'Accept: application/json' \ --compressed