AI-generated Key Takeaways
-
Retrieve a list of food items eaten within a specified time frame by querying for
DataType.TYPE_NUTRITION
in Android. -
Retrieving food items via the REST API involves getting data source IDs, obtaining data from each source, and combining the lists if necessary.
-
To get food data sources using the REST API, send a GET request to the
dataSources
endpoint with thedataTypeName=com.google.nutrition
. -
Obtain a list of food eaten from a specific data source by using the
dataStreamId
and the desired time range in thedatasets
endpoint. -
Field masks can be used to limit the response to only the desired properties when retrieving food data.
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